C lang
Achieve multi-directional branch using switch.
in C lang
Specify variable which has integer on braces after switch, and specify integer, string, or const value. If you want to use another condition, use 'if' and 'else if'.
Process stops at 'break'. If there is no 'break', process continues to next 'case'.
If there is 'default' and the variable does not match any 'case', 'default ' is processed.
switch.c
#include <stdio.h> void switchFunc(int value) { switch(value) { case 1: puts("show 1 and break"); break; case 2: puts("show 2, and no break"); case 3: puts("show 3 and break"); break; default: puts("value does not match the above any case"); break; } return; } int main(void) { switchFunc(1); switchFunc(2); switchFunc(3); switchFunc(99); return 0; }
Result
$ gcc switch.c -o switch $ ./switch show 1 and break show 2, and no break show 3 and break show 3 and break value does not match the above any case