If no match occurs between the controlling expression’s value and a case
label, the default
case (lines 92–95) executes. We use the default
case in this example to process all controlling-expression values that are neither valid grades nor newline, tab or space characters. If no match occurs, the default
case executes, and lines 93–94 print an error message indicating that an incorrect letter grade was entered. If no match occurs in a switch
statement that does not contain a default
case, program control continues with the first statement after the switch
.
Error-Prevention Tip 5.3
Provide a default case in switch statements. Cases not explicitly tested in a switch statement without a default case are ignored. Including a default case focuses you on the need to process exceptional conditions. There are situations in which no default processing is needed. Although the case clauses and the default case clause in a switch statement can occur in any order, it’s common practice to place the default clause last.
Good Programming Practice 5.2
The last case in a switch statement does not require a break statement. Nevertheless, include this break for clarity and for symmetry with other cases.