Types of Conditional Statements (switch)

In the last tutorial, we have understood how if-else-if and Nested if are useful in making decisions. Now, in this tutorial, we will understand a very handy way to make a decision called the switch statement.


What’s the problem with if-else-if?

Imagine you are an ice cream shop owner and you have a bunch of flavors to serve your customers. Let’s say, you have vanilla, chocolate, coconut, mango, and banana flavors to offer. The customer has the choice to select any one of these flavors. The following program displays the appropriate message based on the flavor selected by the customer. 

Program:

#include <stdio.h>

int main() {
    int flavor = 4;
    
    if (flavor == 1) {
        printf("preparing vanilla ice cream.");
    } else if (flavor == 2) {
        printf("preparing chocolate ice cream.");
    } else if (flavor == 3) {
        printf("preparing coconut ice cream.");
    } else if (flavor == 4) {
        printf("preparing mango ice cream.");
    } else if (flavor == 5) {
        printf("preparing banana ice cream.");
    }
    
    return 0;
}

Output:

preparing mango ice cream.

In the above code, the customer’s flavor is compared with the available flavors. Out of all the conditions, only the fourth condition is satisfied, and hence the appropriate output is displayed. Only the last condition is not checked because after evaluating the fourth condition, the condition is True and hence, there is no point of checking the last condition.

One can imagine that the code becomes complex to read quickly with more options. Thankfully, in the C programming language, there’s a more readable and maintainable way to handle multiple options: the switch statement. Let’s discuss the structure of the switch statement and how to use it in programs.


The switch Statement

Definition and Syntax

The switch statement allows us to execute a block of code based on the expression passed to it within parentheses. The syntax of the switch statement is as follows:

Syntax:

switch(expression) {
    case value1:
        // statements 1
        break;
        
    case value2:
        // statements 2
        break;
            
    case value3:
        // statements 3
        break;

    default:
        // statements 4
        break;
}

Note that the default statement will only be run if none of the other cases match.

Example:

Let’s consider the same ice cream example, but this time we will use the switch statement in place of the if-else-if statement.

#include <stdio.h>

int main() {
   int flavor = 4;
 
   switch(flavor) {
       case 1:
           printf("preparing vanilla ice cream.");
           break;
     
       case 2:
           printf("preparing chocolate ice cream.");
           break;
         
       case 3:
           printf("preparing coconut ice cream.");
           break;
         
       case 4:
           printf("preparing mango ice cream.");
           break;
         
       case 5:
           printf("preparing banana ice cream.");
           break;
   }
   return 0;
}

Output:

preparing mango ice cream.

The output remains the same, only the structure has been changed. The code itself is self-explanatory.

Why is the switch statement useful?

The switch statement is useful because of the following reasons:

  1. Readability:  when you have many options, the switch statement makes your code more readable and cleaner compared to a bunch of if-else-if statements.
  2. Efficiency:  the switch statement can be more efficient in some cases because it directly jumps to the correct option without evaluating each condition.

At last, note that choosing switch over if-else-if is a personal choice. The general rule of thumb is that If there are many options available, then going with the switch statement is considered a better option.



Leave a comment

Leave a comment

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

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.