Loading...
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
Selects one of many code blocks based on the value of an expression. Cleaner than long else-if chains for discrete values.
switch (expression) {
case value1:
// code
break;
case value2:
// code
break;
default:
// fallback
}switch (day) {
case 1: cout << "Mon"; break;
case 2: cout << "Tue"; break;
case 3: cout << "Wed"; break;
default: cout << "Other";
}Without break, execution falls through to the next case. This is a common bug source.
Switch works with int, char, and enum. It does NOT work with strings or floats.