zettelkasten

Switch Statement

Last updated: 1/9/2025

Switch statement compares one object to a bunch of other cases, these cases must be constant at compiler-time and look like:

c++
switch object {
case case_1:
/*
Code
*/
break;
case case_2: // Follow-through
/*
Code
*/
case case_3:
/*
Code
*/
break;
default:
assert(false);
}

If the case does not have a break then it will continue to the following cases until it sees a break. This is called follow through.

In general, switch statements are cleaner and faster than [[If-Else Statement|if-else statements]], but as mentioned before they cannot handle dynamic cases.

See Also

  1. [[Control Flow]]
  2. [[computer-science]]