C Switch Statement

C Switch Statement: Cleaner Multi-Way Branching

When you have a large number of conditions to check against a single variable, writing a massive if...else if...else ladder can become messy, hard to read, and difficult to maintain.

The switch statement in C solves this problem. It allows you to select one of many code blocks to be executed based on the exact value of an expression. It is an essential tool for creating clean, professional, and efficient control flow.


1. How the Switch Statement Works

The switch statement evaluates a single expression (usually an integer or a character) and compares its value against a series of case labels. When a match is found, the code inside that specific case is executed.

Syntax:

switch (expression) {
  case constant1:
    // Code to be executed if expression == constant1;
    break;
  case constant2:
    // Code to be executed if expression == constant2;
    break;
  default:
    // Code to be executed if no cases match;
}

Important Rules for switch:


2. A Basic Switch Example

Let's look at an example where we print the day of the week based on an integer value.

Day of the Week Calculator

#include <stdio.h>

int main() { int day = 4;

switch (day) { case 1: printf("Monday\n"); break; case 2: printf("Tuesday\n"); break; case 3: printf("Wednesday\n"); break; case 4: printf("Thursday\n"); // This block will execute break; case 5: printf("Friday\n"); break; case 6: printf("Saturday\n"); break; case 7: printf("Sunday\n"); break; }

return 0; }


3. The break Keyword

Notice the break keyword at the end of each case. When the C compiler reaches a break statement, it instantly breaks out of the entire switch block. This prevents the execution from "falling through" to the next case.

If you forget the break, the program will continue executing the code in the next cases, even if their conditions were not met! This is called fall-through.

The Fall-Through Effect (Missing Break)

#include <stdio.h>

int main() { int choice = 2;

switch (choice) { case 1: printf("One\n"); case 2: printf("Two\n"); // Execution starts here case 3: printf("Three\n"); // Continues here because there was no break! }

return 0; }

*Output of the above code will be both "Two" and "Three".* Sometimes fall-through is used intentionally to apply the same code to multiple cases!

4. The default Keyword

The default keyword specifies some code to run if there is no case match. It acts exactly like the final else in an if...else if...else ladder.

Including a default case is considered an industry best practice to handle unexpected or invalid input.

Using Default Example

#include <stdio.h>

int main() { int status_code = 404;

switch (status_code) { case 200: printf("OK - Success\n"); break; case 403: printf("Forbidden\n"); break; case 500: printf("Internal Server Error\n"); break; default: printf("Unknown Status Code\n"); // This will execute }

return 0; }

Note: The default block does not require a break statement because it is usually placed at the very end of the switch, but adding one is totally fine and often done for consistency.


5. Grouping Cases Together (Intentional Fall-through)

If multiple cases should execute the exact same block of code, you can group them together by omitting the break statement between them. This leverages the fall-through behavior we discussed earlier.

Grouping Cases Example

#include <stdio.h>

int main() { char grade = 'B';

switch (grade) { case 'A': case 'B': case 'C': printf("Congratulations, you passed!\n"); break; case 'D': case 'F': printf("Sorry, you failed.\n"); break; default: printf("Invalid grade entered.\n"); }

return 0; }


Switch vs. If...Else

While both control structures achieve similar results, there are reasons to choose one over the other:


Exercise 1 of 2

?

What happens if you forget to include a `break` statement inside a `switch` case?

Exercise 2 of 2

?

Which data type CANNOT be evaluated by a `switch` statement in C?