The decision control structure in C can be implemented using
1. The if Statement
2. The if - else Statement
3. The Nested if - else Statement
The if Statement
The general form of it statement looks like this :
if(this condition is true)
execute this statement;
The if statement by itself will execute a single statement or a group of statement when the condition following if is true.
The simple example of a if statement is :
if(varName = = 20)
printf("Value of the variable is 20");
We can use the block to specify the statement to pre executed if the given condition is true.
if(varName = = 20)
{
printf("Value of the variable is 20");
printf("Print what ever you want !!!");
}
The if - else Statement
The if statement by itself will execute a single statement or a group of statements when the condition following if is true. It does nothing when the condition is false. If the condition is false then a group of statements can be executed using else statement.
The following program illustrates this
/* Calculation of gross salary */
main( )
{
float bs, gs, da, hra;
printf("Enter basic salary");
scanf("%f", &bs);
if(bs<1500)
{
hra = bs * 10/100;
da = bs * 90/100;
}
else
{
hra = 500;
da = bs * 98/100;
}
gs = bs+hra+da;
printf("gross salary = Rs. /.f" , gs);
}
The Nested if - else Statement
It we write an entire if - else construct within the body of the if statement or the body of an else statement. This is called nesting of if . For example
if(condition)
{
if(condition)
{
do this;
}
else
{
do this;
and this;
}
else
do this;
}
1. The if Statement
2. The if - else Statement
3. The Nested if - else Statement
The if Statement
The general form of it statement looks like this :
if(this condition is true)
execute this statement;
The if statement by itself will execute a single statement or a group of statement when the condition following if is true.
The simple example of a if statement is :
if(varName = = 20)
printf("Value of the variable is 20");
We can use the block to specify the statement to pre executed if the given condition is true.
if(varName = = 20)
{
printf("Value of the variable is 20");
printf("Print what ever you want !!!");
}
The if - else Statement
The if statement by itself will execute a single statement or a group of statements when the condition following if is true. It does nothing when the condition is false. If the condition is false then a group of statements can be executed using else statement.
The following program illustrates this
/* Calculation of gross salary */
main( )
{
float bs, gs, da, hra;
printf("Enter basic salary");
scanf("%f", &bs);
if(bs<1500)
{
hra = bs * 10/100;
da = bs * 90/100;
}
else
{
hra = 500;
da = bs * 98/100;
}
gs = bs+hra+da;
printf("gross salary = Rs. /.f" , gs);
}
The Nested if - else Statement
It we write an entire if - else construct within the body of the if statement or the body of an else statement. This is called nesting of if . For example
if(condition)
{
if(condition)
{
do this;
}
else
{
do this;
and this;
}
else
do this;
}
0 Comments:
Post a Comment