if…else statement
if…else statement is an extension of the simple if statement
The general form is:
Syntax:
if(test condition)
{
true-block statement(s)
}
else
{
flase-block statement(s)
}
statement –x;
Example 1:
The program below, is to identify if the values of a and b are equal or not.
#include<stdio.h>
int main(void)
{
float a=12.25, b=13.65; //the values of ‘a’ and ‘b’ are assigned in float.
if(a==b) //’a’ is assigned value of ‘b’. Hence it is not zero it becomes true.
printf(“a and b are equal”);
else //otherwise the output is displayed as a and b are not equal. The catch here is the assignment operator used in the if statement. It simply assigns the value of ‘b’ to ‘a’, and hence the condition becomes, if(13.65).
printf(“a and b are not equal”);
return 0;
}
OUTPUT
a and b are equal
Forms of if statement
1. if(condition)
do this;
2. if(condition)
{
do this;
and this;
}
3. if(condition)
do this;
else
do this;
4. if(condition)
{
do this;
and this;
}
else
{
do this;
and this;
}
5. if(condition)
do this;
else
if(condition)
do this;
else
{
do this;
and this;
}
}
6. if(condition)
{
if(condition)
do this;
else
do this;
and this;
}
}
else
do this;