The if-else statement directs the computer to select a sequence of one or more instructions based on the result of a comparison. When two groups of statements are given and it is desired that one of them be executed if some condition is true and the other group be executed if that condition is not true. In such a situation, the if-else statement can be used. The general format of if-else statement is
if (expression)
{
program statements;
}
else
{
program statements;
}
To understand this programming construct, let us consider the example of checking whether the given number is odd or even. The tasks include
Example 2.3
#include <stdio.h>
int main ()
{
int number;
printf ("Enter the number\n");
scanf ( "%d", &number)
if (number % 2 == 0)
printf ("The number is even");
else
printf ("The number is odd");
}