what do switch case do in c code example
Example 1: switch statement in c
#include <stdio.h>
int main(void)
{
int a = 0;
switch(a)
{
case 1 :
statement("a = 1");
break;
case 2 :
printf("a = 2");
break;
default :
printf("a is neither 1 or 2");
break;
}
}
Example 2: Switch Mode C Programming
//A switch statement allows a variable to be
//tested for equality against a list of values.
#include <stdio.h>
int mode = 2;
int main() {
//switch takes argument and gives cases for it.
switch (mode) {
//each case has a value for which it tests...
case 1:
//Your code goes here...
printf("This is mode #1.\n");
//each case must have break otherwise code will fall
//through into next case. This is good for some things
//like testing multiple case at once on the same value, though.
break;
case 2:
//Your code goes here...
printf("This is mode #2\n");
break;
case 3:
//Your code goes here...
printf("This is mode #3.\n");
break;
}
return 0;
}