definition of switch case in programming language code example

Example: 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;
}

Tags:

C Example