IF ..... ELSE STATEMENT


IF ..... ELSE STATEMENT

The if .... else statement is an extension of the 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 statement(s), immediately following the if statement are executed; otherwise, the false-block statement(s) are executed. In either case, either true-block or false-block will be executed, not  both. In both the cases, the control is transfered subsequently to statement-x.

Expression is true    if

x = = y x is equal to y
x != y x is not equal to y
x < y x is less than y
x > y x is greater than y
x <= y x is less than or equal to y
x >= y x is greater than or equal to y


/* A program that determines if an integer number is odd or even.
 stdio.h>
int main (void)
{
   int number, even;
         /* ask user for number */
   printf ("Enter an integer number: ");
   scanf ("%d", &number);

   /* determine if number is even and put in variable */
   even = (number % 2 == 0);

   /* display report */
   if (even)
        printf ("%d is an even number.\n", number);
        else
        printf ("%d is an odd number. \n", number);
   return (0);
}


Comments