The switch statement is a control statement that allows us to choose only one choice among the many given choices. The expression in switch
evaluates to return an integer value, which is then compared to the values present in different cases.
It executes that block of code that matches the case value. If there is no match, then the default block is executed (if present).
Please refer to the if-else statement before proceeding for better clarity.
Just consider a simple example where we have many students in a class and their role number identifies them, such that each roll number starting from 1 has a name associated starting from alphabet starting from “A”. This problem can be handled by the concept that is “if .. else” statement.
include <stdio.h> void print_name( int roll_number) { if (roll_number == 1) { printf("\n Name is A"); } else if (roll_number == 2) { printf("\n Name is B"); } else if (roll_number ==3) { printf("\n Name is C"); } else { printf("\n Name is D"); } } int main() { int roll_number = 1; print_name(roll_number); return 0; }
So in the above program: Just imagine the number of “if”, “else if”, and “else” conditions, which simply increases the confusion and complexity as the number of conditions increases.
To overcome this problem or to simplify this, C has a multi-way decision statement called Switch-case.
Basic Syntax:
switch (expression)
{
case value1:
block1;
break;
case value2:
block2;
break;
case value3:
blcok3;
break
default:
default block;
break;
}

Same example with switch-case statement
#include <stdio.h> void print_name( int roll_number) { switch(roll_number) { case 1: printf("\n Name is A"); break; case 2: printf("\n Name is B"); break; case 3: printf("\n Name is C"); break; default: printf("\n Name is D"); break; } } int main() { int roll_number = 1; print_name(roll_number); return 0; }
Rules for using a switch statement:
- The expression after the switch statement must yield an integer value that is an integer value, the integer variable, or an expression that evaluates to an integer.
- The case label must be unique and end with a colon.
- The statement can be any valid C statement or expression.
Note:
- Floating numbers or strings cannot be used with a switch statement.
- break statement is used to exit the switch block. No need to use the break after each block but failing to do so will execute all consecutive blocks of code after the matching block.
- The default case is executed when none of the cases match the switch expression. This can be placed anywhere in the switch block. The switch statement works even without the default statement.
- Nesting of the switch statement is possible as it is having multiple switch statements within the switch block adding more complexity to the code.
Difference between switch
and if
if
statements can evaluatefloat
conditions.switch
statements that cannot evaluate float conditions.- if statement can evaluate relational operators. switch statements cannot evaluate relational operators, i.e., they are not allowed in
switch
statement.
Relevant Posts:
Categories: C Language
Leave a Reply