C Switch Statement

The switch statement in C is an alternate to if-else-if ladder statement. It allows a single variable to be compared with several possible constants. It is a multi-way decision that tests whether an expression matches one of the constants(constant integer values), then a execution jump is made to that point. A constant can not appear more than once, and there can only be one default expression.

The syntax of switch statement in is given below:

Syntax
snippet
switch (expression) {
    case const1:
        //statement1 (code to be executed) 
        break; //optional
    case const1:
        //statement2 (code to be executed)
        break; //optional
        ......
        ......
    default:
        statements(code to be executed
            if all cases are not matched)
}

On evaluating the expression, if it matches one of the set of constant integer expressions, the switch branches to the matching case label and executes the statements following that point. Otherwise it jumps to the default label and executes its statements.

break

The statements following a case label are executed until terminated by a break statement, which causes an immediate exit from the switch block. If a break is not encountered, execution will flow on through to the next cases until the end of the block. It is termed fall through and is the default behaviour in a switch.

default
The default label is optional, and if it does not exist, and none of the case labels match, the switch simply performs no action.
Note
  1. The switch expression must be of an integer or character type.
  2. The case value must be an integer or character constant.

Flowchart of switch statement in C

flow of switch statement in c

Let's see a simple example of c language switch statement.

snippet
#include<stdio.h>
int main() {
    int number = 0;
    printf("enter a number:");
    scanf("%d", & amp; number);
    switch (number) {
        case 10:
            printf("number is equals to 10");
            break;
        case 50:
            printf("number is equal to 50");
            break;
        case 100:
            printf("number is equal to 100");
            break;
        default:
            printf("number is not equal to 10, 50 or 100");
    }
    return 0;
}
Output
enter a number:4 number is not equal to 10, 50 or 100
Output
enter a number:50 number is equal to 50

Switch case example 2

snippet
#include <stdio.h>
int main()
{
	int x = 10, y = 5; 
	switch(x>y && x+y>0)
	{
		case 1: 
		printf("hi");
		break; 
		case 0: 
		printf("bye");
		break;
		default: 
		printf(" Hello bye ");
	} 
		
}
Output
hi

C Switch statement is fall-through

In C language, the switch statement is fall through; it means if you don't use a break statement in the switch case, all the cases after the matching case will be executed.

Let's try to understand the fall through state of switch statement by the example given below.

snippet
#include<stdio.h>
int main() {
    int number = 0;

    printf("enter a number:");
    scanf("%d", & amp; number);

    switch (number) {
        case 10:
            printf("number is equal to 10\n");
        case 50:
            printf("number is equal to 50\n");
        case 100:
            printf("number is equal to 100\n");
        default:
            printf("number is not equal to 10, 50 or 100");
    }
    return 0;
}
Output
enter a number:10 number is equal to 10 number is equal to 50 number is equal to 100 number is not equal to 10, 50 or 100
Output
enter a number:50 number is equal to 50 number is equal to 100 number is not equal to 10, 50 or 100

Nested switch case statement

We can use as many switch statement as we want inside a switch statement. Such type of statements is called nested switch case statements. Consider the following example.

snippet
#include <stdio.h>
int main () {

   int i = 10;
   int j = 20;
 
   switch(i) {
   
      case 10: 
         printf("the value of i evaluated in outer switch: %d\n",i);
      case 20:
         switch(j) {
            case 20:
               printf("The value of j evaluated in nested switch: %d\n",j);
         }
   }
   
   printf("Exact value of i is : %d\n", i );
   printf("Exact value of j is : %d\n", j );
 
   return 0;
}
Output
the value of i evaluated in outer switch: 10 The value of j evaluated in nested switch: 20 Exact value of i is : 10 Exact value of j is : 20
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +