GURU NANAK DEV CO-ED POLYTECHNIC

GURU NANAK DEV CO-ED POLYTECHNIC
MAIN BULIDING

Friday, 30 September 2011

(iii) Nested if....else statement



            This statement is formed by joining if..........else statements either in the if block or in the else block or both. The syntax is

if(test condition-1)
    {
            if(test condition-2)
                        {
                               statement block-1;
                        }
            else
{
                               statement block-2;
                        }
            else
{
                               statement block-3;
                        }
            next statement;

            When this statement is executed, the computer first evaluates the value of test condition-1. If it is false control will be transferred to statement block-3. If it is true test condition-2 is evaluated. If it is true statement block-1 is executed and control is transferred to next statement else statement block-2 is executed and control is transferred to next .

Conditions
(i)         The opening and closing brackets {} are must if the statement blocks
contain more than one statement.
            (ii)        The brackets around the test condition are must
            (iii)       The test Conditions must be a relational or logical expression.
(iv)       The statement blocks are called body of the if.......else statement. It can
contain one or more statements.
(v)        No statements other than the statements in the statement blocks are
allowed between if........else.
(vi)       Each else must match with the nearest if preceding it, which had not
already been matched by an else.

Program to find the biggest of 3 numbers
            #include<stdio.h>
            main ( )
                        {
                                    int a, b, c;
                                    printf(“Enter three numbers”);
                                    scanf(“%d  %d  %d”, &a, &b, &c);
                        if (a>b)
 
                                    {
if (a>c)
                                  printf(“big=%d”, a);
                                   
                        else
                                  printf(“big=%d”, c);
            else
                                   {
                       if(b>c)
                                  printf(“big=%d”, b);

                      else
                                  printf(“big=%d”, c);
                                    }
                                    }
            }
This program is used to find out the biggest of three numbers. Here if......else statements are included in the if block and else blocks.