This statement is used to control the flow of execution of statements in a program.
There are three different types of control statements :-
1.
Decision making statement/ conditional statement
/ selection statements
2.
Looping statements / Iterative statements
3.
Jumping statements / Ad-hoc statements
Decision making stmt.
|
Looping/Iterative stmt.
|
Jumping stmt.
|
-
if…else
-
switch
|
-
while
-
do..while
-
for
|
-
break
-
continue
-
goto
|
Decision making statement :-
In this statement we can take a decision for
executing block of statements. In this case condition is checked. If the
condition is true then true part will execute otherwise false part will
execute.
There are five types of decision making statements .
I. Simple if statement :-
In this statement if condition is true then
true part will execute otherwise no action will generate .
syntax:
if(condition)
eg:-
II. If-else statement :-
In this statement if condition is true then
true part will execute otherwise false part will execute.
syntax:
eg:-
if(n>0)
{
printf( “positive number”);
}
else
{
printf(“ negative number”);
}
eg2:-
if(n%2 == 0)
{
printf( “ even “);
}
else
{
printf( “ odd “ );
}
III. if-else if statement:-
It is generally used to specify more than
one condition . In this case if condition is true then true part will execute
after that terminate from if statement , otherwise next condition is checked.
syntax:
eg:-
p=
85;
if(p>90)
printf(“ excellent “);
else if( p<90 && p>= 75)
printf(“ very good “);
else if ( p<75 && p>=60)
printf(“ good “);
else
printf( “poor “);
OR:
if(p>=90)
printf(“ excellent “);
else if (p >= 75)
printf( “ very good “ );
else if( p> =60)
printf( “good “);
else
printf(“poor”);
IV. Nested if statement :-
When one if statement is defined within
another if statement called nested if.
In this case one is called outer if and
another is called inner if.
syntax:
eg:-
n= -10
if ( n%2 == 0)
{
if
(n > 0)
printf( “ +ve even no.” );
else
printf( “ –ve even no. “);
}
else
{
if (n> 0)
printf( “ +ve odd “ );
else
{
printf( “ –ve odd “ );
}
We will practice on related programs on the next blog.
No comments:
Post a Comment