# The switch Statement – Another Way to Make Decisions

> <div data-node-type="callout">
> <div data-node-type="callout-emoji">💡</div>
> <div data-node-type="callout-text">I recently started teaching a course on C programming and as part of my preparation for class, I usually write out lesson notes that I will be teaching from. I have therefore decided to make these into blog posts so anyone else learning C can also benefit from them. Therefore, this lesson is part of the series I am calling: <strong>C Programming for Absolute Beginners. </strong>I hope you enjoy it. Ask any questions or leave comments if you need further clarification or want to make a suggestion.</div>
> </div>

So far, you’ve learned:

*   `if`
    
*   `if–else`
    
*   `else if`
    
*   Loops
    

Now we’ll learn another decision-making tool in C:

The `switch` **statement**.

It’s useful when you want to compare **one variable against many possible values**. You can find the [previous lesson here](https://blog.ehoneahobed.com/loops-in-c-how-programs-repeat-tasks).

## 1\. When Should You Use `switch`?

Imagine this situation:

*   If the user enters 1 → Print “Monday”
    
*   If the user enters 2 → Print “Tuesday”
    
*   If the user enters 3 → Print “Wednesday”
    
*   And so on...
    

You *could* use many `else if` statements.

But `switch` makes it cleaner and easier to read.

## 2\. Basic Syntax

```c
switch (variable) {
    case value1:
        // code
        break;

    case value2:
        // code
        break;

    default:
        // code
}
```

Important parts:

*   `switch` checks one variable.
    
*   `case` represents possible values.
    
*   `break` stops execution.
    
*   `default` runs if no case matches.
    

## 3\. Simple Example: Day of the Week

```c
#include <stdio.h>

int main() {
    int day;

    printf("Enter a number (1–3): ");
    scanf("%d", &day);

    switch (day) {
        case 1:
            printf("Monday\n");
            break;

        case 2:
            printf("Tuesday\n");
            break;

        case 3:
            printf("Wednesday\n");
            break;

        default:
            printf("Invalid number\n");
    }

    return 0;
}
```

How it works:

*   If `day` is 1 → Monday prints.
    
*   If `day` is 2 → Tuesday prints.
    
*   If `day` is 3 → Wednesday prints.
    
*   Any other number → “Invalid number”.
    

## 4\. Why Is `break` Important?

If you remove `break`, C continues executing the next cases.

Example without `break`:

```c
case 1:
    printf("Monday\n");

case 2:
    printf("Tuesday\n");
```

If `day = 1`, it will print:

```plaintext
Monday
Tuesday
```

This is called **fall-through behavior**.

Most of the time, you want to use `break`.

## 5\. `switch` vs `if–else`

Use `switch` when:

*   You are checking one variable.
    
*   You are comparing it to specific values.
    
*   The values are integers or characters.
    

Use `if–else` when:

*   You are checking ranges (like score ≥ 50).
    
*   You are using logical operators (`&&`, `||`).
    

Example:

This works well with `if–else`:

```c
if (score >= 80)
```

But this works better with `switch`:

```c
switch (choice)
```

## 6\. Example with Characters

`switch` also works with `char`.

```c
#include <stdio.h>

int main() {
    char grade;

    printf("Enter grade (A, B, C): ");
    scanf(" %c", &grade);

    switch (grade) {
        case 'A':
            printf("Excellent\n");
            break;

        case 'B':
            printf("Good\n");
            break;

        case 'C':
            printf("Average\n");
            break;

        default:
            printf("Invalid grade\n");
    }

    return 0;
}
```

Notice the space before `%c` in `scanf`.

That prevents input problems with leftover newline characters.

## 7\. Common Beginner Mistakes

1.  Forgetting `break`
    
2.  Using `switch` for ranges (which doesn’t work)
    
3.  Forgetting `default`
    
4.  Missing the space before `%c` in `scanf`
    

## 8\. Mini Project: Simple Calculator

Let’s combine what you’ve learned.

```c
#include <stdio.h>

int main() {
    int num1, num2;
    char operator;

    printf("Enter first number: ");
    scanf("%d", &num1);

    printf("Enter operator (+ or -): ");
    scanf(" %c", &operator);

    printf("Enter second number: ");
    scanf("%d", &num2);

    switch (operator) {
        case '+':
            printf("Result: %d\n", num1 + num2);
            break;

        case '-':
            printf("Result: %d\n", num1 - num2);
            break;

        default:
            printf("Invalid operator\n");
    }

    return 0;
}
```

Now your program:

*   Takes input
    
*   Uses conditions
    
*   Uses `switch`
    
*   Performs operations
    

That’s real programming.

## 9\. Practice Exercises

1.  Build a menu program:
    
    *   1 → Add
        
    *   2 → Subtract
        
    *   3 → Exit
        
2.  Create a program that:
    
    *   Asks for a number (1–7)
        
    *   Prints the day of the week
        
3.  Modify the calculator to include multiplication.
    

## Final Thoughts

You now understand:

*   `if`
    
*   `if–else`
    
*   `else if`
    
*   `switch`
    
*   Loops
    
*   Input and output
    

At this stage, you are no longer just writing simple code.

You are building logic.

Next, we’ll start organizing code better using **functions**, so your programs become cleaner and easier to manage.
