switch statement – free C programming tutorial

In this tutorial, you will be familiarize with one type of conditional statement that is switch statement available for C programming language, along with its syntax and example of switch .

Conditional statements

Conditional statements are those code which allows you to control the program’s flow of the execution centred on a condition.
It means that the statements in the program are not executed in sequence.
Rather, one group of statements is executed, depending on how the condition is evaluated.


Switch statement:

 Syntax :

switch (expression) 
 { 
     case    label: statements;  
                            break;
     case    label: statements;    // optional  
                            break;
     case    label: statements;    // optional
                            break;  
     case    label: statements;   // optional 
                            break; 
// you can add numerous cases
     default      : statements;   
}

* Expression written in the switch must results in integer value.
* Otherwise default statement executes if present in the switch statement.
* break statement must be written in the end in each case.
* break causes the execution to jump out of the switch to the statement immediately following the switch.


Example of switch statement:

C program which determines whether the entered character by user is a vowel or a consonant.

// Header file which contains functions for standard input and output manipulative operation.
 #include <stdio.h> 
// Compiling of code start from main function   
 int main()  
{ 
// Declaration of local variable of data-type char
    char ch;

// To display on console screen 
   printf(“Enter any character in lower case…… /n ”);

 // To get input from user 
   scanf(“%c”,&ch);

   switch (ch) 
      {      
            case 'a': printf(“Entered character is a vowel /n ”);
                               break;         
            case 'e': printf(“Entered character is a vowel /n ”);
                               break;  
            case 'i': printf(“Entered character is a vowel /n ”);
                               break;         
            case 'o': printf(“Entered character is a vowel /n ”);
                               break;  
            case 'u': printf(“Entered character is a vowel /n ”);
                               break;  
       // you can add numerous cases

            default      : printf(“Entered character is a consonant /n ”);  
      }
  return 0;
}

Here I include both the possible aspects for this code such as entered character is a vowel like ‘a’or it may be a consonant like ‘g’.
Output of both the cases shown below :

case 1.  if entered entered character is a vowel like ‘a’ then the above code is compiled and executed, it produces the following result −
OUTPUT:

Enter any character in lower case……a
Entered character is a vowel

But,
case 2. if entered entered character is a a consonant like ‘g’ then the above code is compiled and executed, it produces the following result −
OUTPUT: