ITP Unit-II LM
ITP Unit-II LM
Objective:
Learning Outcomes:
CONTROL STATEMENTS:
SELECTION STATEMENTS:
if statement
switch statement
1. if Statement:
An if statement consists of a Test expression followed by one or more
statements. It is a one-way decision statement.
Syntax:
if(Text Expr)
{
/*statement(s) will execute if the Test expression is true*/
}
If the Test expression evaluates to true, then the block of code
inside the 'if' statement will be executed.
If the Test expression evaluates to false, then the first set of code
after the end of the 'if' statement (after the closing curly brace) will
be executed.
Flow Diagram
Example
#include<stdio.h>
void main()
{
int a=10; /*local variable declaration*/
if(a<20)
{
printf(“a is less than 20\n”);
}
printf(“The value of a :%d”,a);
}
When the above code is compiled and executed, it produces the following
result −
a is less than 20
The value of a: 10
C programming language assumes any non-zero and non-null values
as true, and if it is either zero or null, then it is assumed as false value.
2. if-else statement:
It is a two-way decision statement. Similar to one-way decision, the
decision here is based on the test expression.
Syntax
if(Text Expr)
{
/*statement(s) will execute if the Test expression is true*/
}
else
{
/*statement(s) will execute if the Test expression is false*/
}
Flow Diagram
Example
#include<stdio.h>
void main()
{
int a=10; /*local variable declaration*/
if(a<20)
{
printf(“a is less than 20\n”);
}
else
{
printf(“a is not less than 20\n”);
}
}
When the above code is compiled and executed, it produces the following
result
a is less than 20
When using if-else-if statements, there are few points to keep in mind −
An if can have zero or one else's and it must come after any else
if's.
An if can have zero to many else if's and they must come before
the else.
Syntax
if(Test Expr1)
{
/*statement(s) will execute if the Test expression1 is true*/
}
else if(Test Expr2)
{
/*statement(s) will execute if the Test expression2 is true*/
}
.
.
else if(Test ExprN)
{
/*statement(s) will execute if the Test expressionN is true*/
}
else
{
/*executes when the none of the above condition is true*/
}
Flow Diagram
Test
Expr1
Test
Expr2
Statement(s) 1
Test
ExprN
Statement(s) 2
else block
Statement(s) N
Example
#include<stdio.h>
void main()
{
int a=100; /*local variable declaration*/
if(a= =20)
{
printf(“Value of a is 20\n”);
}
else if(a==30)
{
printf(“Value of a is 30\n”);
}
else if(a==40)
{
printf(“Value of a is 40\n”);
}
else
{
printf(“None of the values is matching”);
}
}
When the above code is compiled and executed, it produces the following
result
4. Nested if statement
When any if statement is written under another if statement,
this cluster is called nested if.
It is always legal in C programming to nest if-else
statements, which means you can use one if or else if
statement inside another if or else if statement(s).
Syntax
if(Test Expr1)
if(Test Expr2)
Example
#include<stdio.h>
void main ( )
{
int a, b, c;
printf (“\n enter three numbers :”);
scanf (“%d%d%d ”, &a,&b,&c);
if(a > b)
{
if(a>c)
{
printf(“\n a is big”);
}
else
{
printf(“\n c is big”);
}
}
else
{
if (b > c)
{
printf(“\n b is big”);
}
else
{
printf(“\n c is big”);
}
}
When the above code is compiled and executed, it produces the
following result :
Enter three numbers:
264
b is big
if(condition)
if(condition)
else
printf(“dangling else!”);
if(condition)
if(condition)
if(condition)
}
else
switch statement:
A switch case statement is a multi-way decision statement that is
simplified version of if-else block that evaluates only one variable.
Syntax:
switch (expr)
break;
break;
-------------
-------------
default: stmtListN;
Flow Diagram
Example
#include<stdio.h>
#include<conio.h>
void main()
int a,b,ch,c;
clrscr();
scanf(“%d%d”,&a,&b);
printf(“\n1.Addition\t2.Subtraction\t3.Multiplication\t4.Division”)
;
scanf(“%d”,&ch);
switch(ch)
case 1: c=a+b;
break;
case 2: c=a-b;
break;
case 3: c=a*b;
break;
case 4: c=a/b;
break;
printf(“\nresult=%d”,c);
getch();
When the above code is compiled and executed, it produces the following result
ITERATION STATEMENTS:
Iterative statements are used to repeat the execution of a list of
statements, depending on the value of an integer expression.
while loop
do-while loop
for loop
1. while loop
The while loop provides a mechanism to repeat one or more
statements while a particular condition is true.
Syntax:
statement x;
while(condition)
{
statement block;
}
statement y;
Flow Diagram:
Statement x
T F
Statement block
Statement y
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int a=1;
clrscr();
while(a<=5)
{
printf(“%d\t”,a);
a++;
}
}
When the above code is compiled and executed, it produces the
following result
1 2 3 4 5
2. do-while loop
The do-while loop is similar to the while loop. The only difference is
that in a do-while loop, the test condition is tested at the end of
the loop. Now the test condition is tested at the end, this clearly
means that the body of the loop gets executed at least one time
even if the condition is false.
Syntax:
statement x;
do
{
statement block;
}while(condition);
statement y;
Disadvantage: The major disadvantage of using a do-while loop is
that it always executes at least once, even if the user enters some
invalid data.
Flow Diagram
Statement x
Statement block
Condition
T
F
Statement y
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr( );
do
{
printf(“%d\t”,i);
i++;
}while(i<=5);
}
When the above code is compiled and executed, it produces the
following result
1 2 3 4 5
3. for loop
A for loop is a repetition control structure that allows you to
efficiently write a loop that needs to execute a specific number of
times.
Syntax:
for( initialization; condition; increment/decrement/update)
{
statement block;
}
statement y;
Flow Diagram:
Initialization of loop
variable
Controlling F
condition for
loop variable
Statement block
Statement y
The initialization step is executed first, and only once. This step
allows you to declare and initialize any loop control variables. You
are not required to put a statement here, as long as a semicolon
appears.
After the body of the 'for' loop executes, the flow of control jumps
back up to the increment statement. This statement allows you
to update any loop control variables. This statement can be left
blank, as long as a semicolon appears after the condition.
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for( i=1; i<=5; i++)
{
printf(“%d\t”,i);
}
}
When the above code is compiled and executed, it produces the
following result
1 2 3 4 5
Nested loops
C programming allows us to use one loop inside another loop. The
following section shows few examples to illustrate the concept.
Note: you can put any type of loop inside any other type of loop. For
example, a 'for' loop can be inside a 'while' loop or vice versa.
JUMP STATEMENTS:
Jump statements cause an unconditional jump to another statement
elsewhere in the code. They are used primarily to
interrupt switch statements and loops. The jump statements are:
1. break statement
2. continue statement
1. break statement :
The break statement in C programming has the following two usages −
If you are using nested loops, the break statement will stop the execution
of the innermost loop and start executing the next line of code after the
block.
Syntax
break;
Flow Diagram
Example
#include<stdio.h>
void main()
{
int i=0;
while(i<=10)
{
if(i= = 5)
{
break;
}
printf(“\t%d”,i);
i=i+1;
}
}
When the above code is compiled and executed, it produces the
following result
0 1 2 3 4
2. continue statement :
The continue statement in C programming works somewhat like
the break statement. Instead of forcing termination, it forces the next
iteration of the loop to take place, skipping any code in between.
Syntax :
continue;
Flow Diagram
Example
#include<stdio.h>
void main()
{
int i=0;
while(i<10)
{
i=i+1;
if(i= = 5)
continue;
printf(“\t%d”,i);
}
}
When the above code is compiled and executed, it produces the
following result
1 2 3 4 6 7 8 9 10
Problem Solving:
#include <stdio.h>
main()
float a=0,u;
system("cls");
scanf("%f", &u);
if(u<=50)
a= u* 0.75;
a=0.75 * 50 + 0.85*(u-50);
else
a=a+(0.2*a);
getch();
Output:
#include"stdio.h"
main()
int a,b;
char ch;
clrscr();
scanf("%d%d",&a,&b);
ch=getche();
switch(ch)
break;
break;
break;
break;
getch();
Output:
Enter Ur choice:
#include"stdio.h"
#include"math.h"
main()
float a,b,c,d,r1,r2;
clrscr();
scanf("%f%f%f",&a,&b,&c);
d=b*b-4*a*c;
if(d==0)
r1=r2=-b/(2*a);
else if(d<0)
r1=-b/(2*a);
r2=sqrt(-d)/(2*a);
printf("\n r1=%f+i%f",r1,r2);
printf("\n r2=%f-i%f",r1,r2);
else
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("\n r1=%f",r1);
printf("\n r2=%f",r2);
getch();
Output:
r2=-1.000000-i1.414214
#include<stdio.h>
void main()
int n,s=0,k;
system("cls");
printf("Enter an integer:");
scanf("%d",&n);
k=n;
while(n>0)
s=s*10+n%10;
n/=10;
if(k==s)
printf("\n%d is Palindrome",k);
else
getch();
Output:
121 is Palindrome
5. Write a C program to find out given number is prime or not.
#include<stdio.h>
void main()
int n,c=0,i;
clrscr();
printf("\nEnter an Integer:");
scanf("%d",&n);
for(i=1;i<=n;i++)
if(n%i==0)
c++;
else
getch();
Output:
Enter an Integer: 7
7 is Prime Number