Table of Contents
The default label defines the block executed when none of the case labels in a switch statement matches the controlling expression.
#include <stdio.h>
int main()
{
int number = 5;
switch (number)
{
case 1:
printf("One\n");
break;
case 2:
printf("Two\n");
break;
case 3:
printf("Three\n");
break;
default:
printf("Other number\n");
}
return 0;
}
Example Output #
Other number
Explanation #
The switch expression contains the value 5:
switch (number)
There is no case label with the value 5, so none of the case blocks is selected.
Execution therefore continues at the default label:
default:
printf("Other number\n");
The default label is optional. A switch statement can contain no default label, in which case execution continues after the switch when no case matches.
The default label does not have to appear at the end of the switch, although placing it there is conventional.