Control statement:::
THE IF…ELSE
STATEMENT
The if….else statement is an extension of
simple if statement.The general form is
If(test
expression)
{
True-block statement(s)
}
else
{
False-block statement(s)
}
statement-x
If the test expression is
true, then the true block statements are executed; otherwise
the false block statement will be executed
NESTING OF
IF…..ELSE STATEMENTS
When a series of decisions are
involved, we may have to use more than one if….else
statements, in nested form
as follows.
If(test condition
1)
{
if(test condition
2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
statement-3;
}
Example::
Program
/*Selecting the
largest of three values*/
main()
{
float A, B, C;
printf(“Enter three
values \n”);
scanf(“|%f %f
%f”,&A, &B, &C);
printf(“\nLargest
value is:”);
if(A > B)
{ if(A > C)
printf(“%f \n”,A);
else
printf(“%f \n”,C);
}
else
{
if(C > B)
printf(“%f \n”,C);
else
printf(“%f \n”,B);
}
}
OUTPUT
Enter three values:
5 8 24
Largest value is 24
THE ELSEIF
LADDER
The general form is
If(condn 1)
Statement-1;
else if (condn 2)
statement-2;
else if (condn 3)
statement-3;
……….
……….
else if (condn n)
statement-n;
else
default statement;
statement-x;
THE SWITCH
STATEMENT
Switch statement is used for
complex programs when the number of alternatives
increases. The switch statement
tests the value of the given variable against the list of case
values and when a match is found,
a block of statements associated with that case is executed.
The general form of switch
statement is
switch(expression)
{
case value-1:
block-1
break;
case value-2:
block-2
break;
…….
…….
default:
default-block
break;
}
statement-x;
Eg.::
………
index
= marks / 10;
switch(index)
{
case
10:
case
9:
case
8:
grade = “Honours”;
break;
case
7:
case
6:
grade = “first
division”;
break;
case
5:
grade = “second
division”;
break;
case
4:
grade = “third
division”;
break;
default:
grade = “first
division”;
break;
No comments:
Post a Comment
Please comment bellow. Share your knowledge