Control Flow Statements – Part 2

Hello everyone, in this article, I am writing about “Switch case in C” which is another type of control flow statement. If you come here looking for if – else, then go to this article.

Switch Case in C

The syntax of “Switch case” in C is-

switch (expression) {
    case 1:
              break;
     case 2:
              break;
      case n:
               break;
      default:
               break;
}

Here you can see I have written expression. Now the value of this expression can be computed in the program itself or it can be taken by the user as well.

Suppose you want to know if the number entered by the user is divisible by 4. Now, you can easily do this by if-else method but here we are talking about Switch.

To do this, we have to first “scan” a number from the user. Now, there are actually 5 cases. The remainder of the number can be 0, 1, 2, and 3 or there is a chance that the user entered the float number or entered a character. This is the situation where the result doesn’t match any of the cases you defined. To address that, there is a default condition that executes when there is no condition.  Check out the code below.

#include <stdio.h>

int main()
{
	int num = 0;
	printf("Enter a number\n");
	scanf("%d",&num);

	switch(num%4) {
		case 1:
				printf("The Remainder of the number entered when dividing by 4 is 1\n");
				break;
		case 2:
				printf("The Remainder of the number entered when dividing by 4 is 2\n");
				break;
		case 3:
				printf("The Remainder of the number entered when dividing by 4 is 3\n");
				break;
		case 0:
				printf("The Remainder of the number is zero\n");
				break;
		default:
				printf("Please enter an integer number");
                                 break;
	}
	return 0;
}

You might have noticed that I have entered the break statement after each case. Well, check out for yourself what happens when you don’t put break statement.

Nested Switch in C

Just like in if-else, there can be nested switch in C. Any case can have “switch statement” written inside itself as well. For example –

switch(n)
{
  // code to be executed if n = 1;
  case 1: 
    
  // Nested switch
  switch(num) 
  {
    // code to be executed if num = 10
    case 10: 
      statement 1;
      break;
      
    // code to be executed if num = 20
    case 20: 
      statement 2;
      break;
      
    // code to be executed if num = 30
    case 30: 
      statement 3;
      break;
      
      // code to be executed if num 
      // doesn't match any cases
      default: 
  }
  
  
  break;
    
  // code to be executed if n = 2;
  case 2:
    statement 2;
    break;
  
  // code to be executed if n = 3;
  case 3: 
    statement 3;
    break;
  
   // code to be executed if n doesn't match any cases
   default: 
}

 

If-else vs Switch

Now we know about Switch and if-else. Now the question is – which method is better to use and when?

In nested conditions, using “switch” is much better than if – else. Also, in a nutshell-

“If-else conditional branches are great for variable conditions that result in a boolean, whereas switch statements are great for fixed data values.”

For more information about this- you can refer to this awesome post in geeks for geeks.

I guess this is it for this article. Do reach out for feedback in the comment section. It would be really helpful to us. Check out other articles as well.

Leave a Comment

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

Share This