Uses of Missing break Statements

The effect of this switch statement is identical to the previous example, without duplicate statements:

switch (grade)
{
   case 'A' :
   case 'B' :
   case 'C' :
      cout << "Passing" << endl;
      break;

   case 'D' :
   case 'F' :
      cout << "Not Passing" << endl;
      break;

   default        :
      cout << "Invalid grade" << endl;
      break;
}

In the example, the missing break statements allow more than one label to share statements. The equivalent logic can be implemented using an if construct:

if (grade == 'A' || grade == 'B' || grade == 'C')
   cout << "Passing" << endl;
else if (grade == 'D' || grade == 'F')
   cout << "Not Passing" << endl;
else 
   cout << "Invalid grade" << endl;

Previous page

Back to Lesson 8 Index
Back to Outline