Technology

What is Switch statement in C?

The switch statement in C is a list of the values of a particular variable. The switch statement causes control transfer from one statement to the statements based on the condition. And we can say that it is a replacement of the long if statement.

Contents

What is Switch statement  in C

The switch is a control statement in the C  programming language. It is a multiway branch statement where it is easy to execute through different parts of code based on the condition. Here we call each value of the branch as a case, and the variable value varies depending upon switch cases. Observe the flow chart given and follow the syntax to understand the Switch statement concept.

Each and every case in the statement tree is unique. There can be only one default statement for the tree which can appear at any position. Generally, this default statement is at the end of the tree. No case or default statements can be out of the Switch statement scope, as it cannot be executed. To understand better go through the rules for the Switch statement given below.

Syntax

switch (n)
{
case 1: //  if n = 1 code executes;
break;
case 2: // if n = 2 code executes;
break;
default: // code to be executed if n doesn’t match any cases
}

Example:

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

   /* local variable definition */
   char grade = 'B';

   switch(grade) {
      case 'A' :
         printf("Excellent!\n" );
         break;
      case 'B' :
      case 'C' :
         printf("Well done\n" );
         break;
      case 'D' :
         printf("You passed\n" );
         break;
      case 'F' :
         printf("Better try again\n" );
         break;
      default :
         printf("Invalid grade\n" );
   }
   
   printf("Your grade is  %c\n", grade );
 
   return 0;
}

Output:

Well done
Your grade is B

Rules for the Switch statement

  1. Any expression of the Switch should give a result as a constant and unique value, or else it is invalid.
  2. These case values statements should end with the semicolon always(;).
  3. One should not repeat the case values or duplicate the case values to avoid errors.
  4. The ‘default‘ statement is an optional one, without adding it also the Case statement works.
  5. Break‘ statement is mandatory, as it will end the control flow of the statements. Where Statements present after Break are not executed.
  6. Declaring Switch statements inside another Switch is possible, which is known as Nested Switching.

 

Switch statement behavior

Let’s see the working of the Switch statement for different conditions.

 

Condition Action
If the condition value matches with Switch statement values Control is transferred to the matched statement
If the condition value doesn’t matches and if the default value present in the set Now the control is given to the default statement
If the condition value doesn’t matches and the default value is not present

Here the control goes out of the Switch statements

 

Why do we use Switch statements in C?

The If statement in C becomes complex when you have many cases for a single variable and only should be chosen at a time. In Such cases to clearly declare all the cases in one statement Switch statement is very useful. It makes the decision making easy over multiple options and the C code also looks simple.

Conclusion

The Switch statements in C is to choose one condition or value out of listed values of a variable. Do remember that the Switch statement contains executable test-expressions. And Break is a necessary condition for Switch statements. Read the article and compare the concept with the provided example to enhance understanding. Also, know the difference between C and C++ here. Practice some C programs using switch statements in any free compilers.

Leave a Reply

Your email address will not be published. Required fields are marked *