Sunday, October 30, 2011

The Tips and Traps C programing Loop Control Structure

A few useful tips about the usage of switch and a few pitfalls to be avoided:

(a)  The earlier program that used switch may give you the wrong impression that you can use only cases arranged in ascending order, 1, 2, 3 and default. You can in fact put the cases in any order you please. Here is an example of scrambled case order:

 main( )
{
     int  i = 22 ;

     switch ( i ) 
 {
           case 121 :
                 printf ( "I am in case 121 \n" ) ;
   break ;
           case 7 :
                 printf ( "I am in case 7 \n" ) ;
   break ;
           case 22 :
                 printf ( "I am in case 22 \n" ) ;
   break ;
  default :
                 printf ( "I am in default \n" ) ;
 }
}

 The output of this program would be:

 I am in case 22 

(b)  You are also allowed to use char values in case and switch as shown in the following program:

 main( )
{
     char  c = 'x' ;

     switch ( c ) 
 {
           case 'v' :
                 printf ( "I am in case v \n" ) ;
   break ;
           case 'a' :
                 printf ( "I am in case a \n" ) ;
   break ;
           case 'x' :
                 printf ( "I am in case x \n" ) ;
   break ;
  default :
                 printf ( "I am in default \n" ) ;
 }
}

 The output of this program would be:

 I am in case x 

In fact here when we use ‘v’, ‘a’, ‘x’ they are actually replaced by the ASCII values (118, 97, 120) of these character constants.

(c)  At times we may want to execute a common set of statements for multiple  cases. How this can be done is shown in the following example.

 main( )
{
     char  ch ;

     printf ( "Enter any of the alphabet a, b, or c " ) ;
     scanf ( "%c", &ch ) ;
switch ( ch ) 
 {
           case 'a' :
           case 'A' :
                 printf ( "a as in ashar" ) ;
   break ;
           case 'b' :
           case 'B' :
                 printf ( "b as in brain" ) ;
   break ;
           case 'c' :
           case 'C' :
                 printf ( "c as in cookie" ) ;
   break ;
  default :
                 printf ( "wish you knew what are alphabets" ) ;
 } 
}

 Here, we are making use of the fact that once a case is satisfied the control simply falls through the case till it
doesn’t encounter a break statement. That is why if an alphabet a is entered the case ‘a’ is satisfied and since there are no statements to be executed in this case the control automatically reaches the next case i.e. case ‘A’ and executes all the statements in this case.

(d)  Even if there are multiple statements to be executed in each case there is no need to enclose them within a pair of braces (unlike if, and else). 

(e)  Every statement in a switch must belong to some case or the other. If a statement doesn’t belong to any case the compiler won’t report an error. However, the statement would never get executed. For example, in the following program the printf( ) never goes to work.
main( )
{
     int  i, j ;

     printf ( "Enter value of i" ) ;
     scanf ( "%d”, &i ) ;

     switch ( i )
 {
           printf ( "Hello" ) ;
           case 1 :
                 j = 10 ;
   break ;
           case 2 :
                 j = 20 ;
   break ;
 }
}

(f)  If we have no default case, then the program simply falls through the entire switch and continues with the next instruction (if any,) that follows the closing brace of switch.

(g)  Is switch a replacement for if? Yes and no. Yes, because it offers a better way of writing programs as compared to if, and no because in certain situations we are left with no choice but to use if. The disadvantage of switch is that one cannot have a case in a switch which looks like:

        case i <= 20 :

 All that we can have after the case is an int constant or a char constant or an expression that evaluates to one of these
constants. Even a float is not allowed.  The advantage of switch over if is that it leads to a more structured program and the level of indentation is manageable, more so if there are multiple statements within each case of a switch.

(h)  We can check the value of any expression in a switch. Thus
the following switch statements are legal.

       switch ( i + j * k )
       switch ( 23 + 45 % 4 * k )
       switch ( a < 4 && b > 7 )

 Expressions can also be used in cases provided they are constant expressions. Thus case 3 + 7 is correct, however, case a + b is incorrect. 

(i)  The break statement when used in a switch takes the control outside the switch. However, use of continue will not take the control to the beginning of switch as one is likely to believe.

(j)  In principle, a switch may occur within another, but in practice it is rarely done. Such statements would be called nested switch statements.

(k)  The  switch statement is very useful while writing menu driven programs. This aspect of switch is discussed in the exercise at the end of this chapter

No comments:

Post a Comment