GURU NANAK DEV CO-ED POLYTECHNIC

GURU NANAK DEV CO-ED POLYTECHNIC
MAIN BULIDING

Friday, 30 September 2011

(iii) else.....if statement


else.....if adder statement is used to take multi-way decision. This statement is formed by joining if.......else statements in which each else contains another if......else. The syntax is,
            if(test condition-1)
                        {
                                    statement block-1;
                        }
            else if (test condition-2)
{
                                    statement block-2;
                        }





                       
            else if (test condition-n)
{
                                    statement block-n;
                        }
            else
                        default statement;
            next statement;

The Computer executes this statement from top to bottom. If a true test condition is found, the statement block associated with it is executed; Then the control is transferred to the next statement. When all the test conditions are false, then the final else containing the default statement will be executed.

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)       Statement blocks are called body of the if..........else statement. It can
contain one or more statements.
(v)        Default statement is a must. If not present no action will take place if all
test Conditions are false.
(vi)       No statements other than the statements in the statement blocks are
allowed between if......else.


Example
 To display the day, depending upon the number entered.
            #include<stdio.h>
            main ( )
            {
                        int day;
                        printf(“Give a number between 1 and 7\n”)
                        scanf(“%d”, &day);
                        if (day = = 1)
                                    printf(“Monday \n”);
            elseif (day = =2)
                                    printf(“Tuesday \n”);
            elseif (day = =3)
                                    printf(“Wednesday \n”);
elseif (day = =4)
                                    printf(“Thursday \n”);
elseif (day = =5)
                                    printf(“Friday \n”);
elseif (day = =6)
                                    printf(“Saturday \n”);
            else
                        printf(“Sunday \n”);
}