C Switch statement is a conditional statement, which is used to choose one of the action to perform from the multiple actions, relative to the true condition.
Syntax:
switch (expression)
{
case value1:
code to be executed if expression=value1;
break;
case value2:
code to be executed if expression=value2;
break;
case value3:
code to be executed if expression=value3;
break;
…
default:
code to be executed if expression is different from all values;
}
Example:
#include <stdio.h> void main() { int num = 5; switch (num) { case 1: printf ("One!"); break; case 2: printf ("Two!"); break; case 3: printf ("Three!"); break; case 4: printf ("Four!"); break; case 5: printf ("Five!"); break; default: printf ("Other!"); } } |
Output
Five! |