Lesson SWITCH Stmt
Lesson SWITCH Stmt
However, the syntax of the switch statement is much easier to read and write.
The example below uses the weekday number to display the weekday name
int day = 4;
switch (day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;
}
A break can save a lot of execution time because it "ignores" the execution of all the rest of the code in
the switch block.