C++ Notes: Switch statement

The switch statement chooses statements to execute depending on an integer value. The same effect can be achieved with a series of cascading if statements, but in some cases the switch statement is easier to read, and some compilers will produce more efficient code. The break statement exits from the switch statement. If there is no break at the end of a case, execution continues in the next case, which is usually an error.
switch (expr) {
  case c1:
        statements  // do these if expr == c1
        break;
        
  case c2: 
        statements  // do these if expr == c2
        break;
        
  case c2:  // multiple values can share same statements
  case c3:
  case c4:
        statements  // do these if expr == any of c2, c3, or c4
        break;
        
  default:
        statements  // do these if expr != any above
}

Equivalent to if statement

The example switch statement above can be written as the following if:
if (expr==c1) {
    statements
} else if (expr==c2) { 
    statements
} else if (expr==c2 || expr==c3 || expr==c4) {
    statements
} else {
    statements
}

The unusual situation of one case falling into another without a break statement can not be as easily translated into an if statement; either extra tests or the forbidden goto must be used.