Switch –case statement generally to check the value of an integer with different discrete possibilities. Let us plan to have a menu driven program where there is a variable called int menu_selection, which can have values like 0, 1, 2, 3, -1, etc as user selection value. Now say 0 means new student addition, 1 means edit record, 2 means display record and -1 means exit to previous menu. Here we trying to implement this with if-else-if statement.

if (menu_selection == -1) {
   printf ("Exitting.\n");
} else if (menu_selection == 0) {
  add_record ();
 
} else if (menu_selection == 1) {
10 
11    edit_record ();
12    
13  } else if (menu_selection == 2) {
14 
15    display_record ();
16    
17  } else if (menu_selection == 3) {
18 
19    delete_record ();
20    
21  } else {
22 
23    printf ("Invalid selection.\n");
24    
25  }

This type of comparison with discrete values is best suited for a switch case. Now we will re-write the above logic in a more managed way. Here point to remember is if-else-if statements are used mainly to check integer range values and switch is used to check distinct values.

switch (menu_selection) {
  
  case -1:
  printf ("Exitting.\n");
  break;
  
  case 0
  add_record ();
  break;
10    
11    case 1
12    edit_record ();
13    break;
14    
15    case 2
16    display_record ();
17    break;
18    
19    case 3
20    delete_record ();
21    break;
22    
23    default:
24    printf ("Invalid selection.\n");
25  }
Here we have a default case which is similar to that last else statement. Control generally goes to this case when there is no match found in all cases. It is always recommended to put a break statement at the end of each case to send the control to the end of switch.

switch (<variable>) {
  
  case <value>:
  <statement (s)>
  break;
  
  case <value>:
  <statement (s)>
  break;
10    
11    ...
12    
13 
14    default:
15    <statement (s)>
16  }
17 
18 
19  switch (<variable>) {
20    
21    case <value>{
22      <statement (s)>
23      break;
24    }
25    
26    case <value>{
27      <statement (s)>
28      break;
29    }
30    
31    ...
32    
33 
34    default: {
35      <statement (s)>
36    }
37  }
38 

Please note: Placing a break at the end of a case is optional. Not putting break in one case causes control to go to the next case block unconditionally. In our example we can remove the break after edit_record(). This will take the control to go to display_record (); after an edit.

switch (menu_selection) {
  
  case -1:
  printf ("Exitting.\n");
  break;
  
  case 0
  add_record ();
  break;
10    
11    case 1
12    edit_record (); /* No break */
13    
14    case 2
15    display_record ();
16    break;
17    
18    case 3
19    delete_record ();
20    break;
21    
22    default:
23    printf ("Invalid selection.\n");
24  }

Also note there is no break at the end of default case. Default case is the last case block thus control is supposed to go to the end of switch and hence break is not required here. Even if putting a break will not cause any error either.

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

const, sizeof void, if, if-else, if-else-if, label and goto, switch case, compare with ==, execution sequence, loop statements,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan