GURU NANAK DEV CO-ED POLYTECHNIC

GURU NANAK DEV CO-ED POLYTECHNIC
MAIN BULIDING

Friday, 30 September 2011

(iv) Switch Statement


Switch statement is an extension of if.......else statement. This permits any number of branches. The syntax is
     
                   switch(expression)
                        {
                                    case label1:
                                                statement block-1;
                                                break;
                        case label2:
                                                statement block-2;
                                                break;
                                    --------------------------
                        --------------------------
                        case lablen:
                                                statement block-n;
                                                break;
                                    default :
                                    default statement
                                                break;
                        }
            next statement;

When this statement is executed the computer first evaluates the value of the expression in the keyword switch. This value is successively compared with the case label1, label2.....labeln. If a case label matches with the value, the statement block associated with the case label is executed. Then the control is transferred to the next statement.

If none of the case matches with the value, the default statement block is executed.


Conditions
            (i)         The body of the switch statement should be enclosed within {} brackets.
            (ii)        The expression should be placed in parentheses.
(iii)       Break statement is a must and causes immediate exit from switch
statement. If it is not included the statements below the matched case will be executed.
            (iv)       The value of the expression should be an integer.
            (v)        Case label should terminate with a colon




Example
 To display the depending upon the numbered entered
#include<stdio.h>
main ( )
            {
            int day;
            printf(“Enter a number between 1 and 7\n”)
                        scanf(“%d”, &day);
                        switch (day)
                        {
                        case 1:
                        printf (“Monday \n”);
                        break;
case 2:
                        printf (“Tuesday \n”);
                        break;
case 3:
                        printf (“Wednesday \n”);
                        break;
case 4:
                        printf (“Thursday \n”);
                        break;
case 5:
                        printf (“Friday \n”);
                        break;
case 6:
                        printf (“Saturday \n”);
                        break;
case 7:
                        printf (“Sunday \n”);
                        break;
default:
                        printf (“Enter a correct number \n”);
                        break;
                        }
            }