Thursday 9 June 2011

Switch Case in C


    The basic form of a switch statement is:
    switch (variable) { case expression1: do something 1; break; case expression2: do something 2; break; .... default: do default processing; }
     In the c program below, if you enter 5 , it will display you entered 5, else if the condition fails it will display the default , that is "i don't know what u entered". The break statement terminates the loop.
    Another Example of Switch Case Program:
    switch( marks )      {         case 'A' : printf( "A++" );                    break;         case 'B' : printf( "A+" );            break; case 'C' : printf( "A" );            break;         case 'D' : printf( "B" );            break; case 'F' : printf( "C" );               break; default  : printf( "YOU FAILED" );                    break; }
    The keyword break must be included at the end of each case statement. The default clause is optional
    We also have a continue statement which is used with switch case.
    A continue statement causes a jump to the loop-continuation portion of the smallest enclosing iteration statement; that is, to the end of the loop body.

0 comments:

Post a Comment