C Switch


The switch statement allows a variable to be checked for equality against a list of values that are defined in the code.


Each value is defined as the case, and the variable being switched is checked for each switch case.

C Switch Statement

Syntax:-

switch(expression) {
	case constant-expression  :
	statement(s);
	break;	/* optional */
	
    case constant-expression  :
	statement(s);
	break;	/* optional */
 
  	/* you can have N number of case statements */
    
  	default :	/* Optional */
	statement(s); /* If the above case matched or not. Default case always to be executed. */
}

Example:-

#include <stdio.h> 
void main(){    
int a=0;     
printf("Enter the value of a:");    
scanf("%d",&a);    
    switch(a){    
        case 0:    
        printf("value is zero");    
        break;    
        case 1:    
        printf("value is one");    
        break;    
        case 100:    
        printf("value is hundred");    
        break;    
        default:    
        printf("value is not 10, 50 or 100");    
    }   
}



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.