Skip to main content

Command Palette

Search for a command to run...

The switch Statement – Another Way to Make Decisions

Published
4 min read
D

Heya! 👋 I love helping people, and one of the best ways I do this is by sharing my knowledge and experiences. My journey reflects the power of growth and transformation, and I’m here to document and share it with you.

I started as a pharmacist, practicing at a tertiary hospital in the Northern Region of Ghana. There, I saw firsthand the challenges in healthcare delivery and became fascinated by how technology could offer solutions. This sparked my interest in digital health, a field I believe holds the key to revolutionizing healthcare.

Determined to contribute, I taught myself programming, mastering tools like HTML, CSS, JavaScript, React, PHP, and more. But I craved deeper knowledge and practical experience. That’s when I joined the ALX Software Engineering program, which became a turning point. Spending over 70 hours a week learning, coding, and collaborating, I transitioned fully into tech.

Today, I am a Software Engineer and Digital Health Solutions Architect, building and contributing to innovative digital health solutions. I combine my healthcare expertise with technical skills to create impactful tools that solve real-world problems in health delivery.

Imposter syndrome has been part of my journey, but I’ve learned to embrace it as a sign of growth. Livestreaming my learning process, receiving feedback, and building in public have been crucial in overcoming self-doubt. Each experience has strengthened my belief in showing up, staying consistent, and growing through challenges.

Through this platform, I document my lessons, challenges, and successes to inspire and guide others—whether you’re transitioning careers, exploring digital health, or diving into software development.

I believe in accountability and the value of shared growth. Your feedback keeps me grounded and motivated to continue this journey. Let’s connect, learn, and grow together! 🚀

💡
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: C Programming for Absolute Beginners. I hope you enjoy it. Ask any questions or leave comments if you need further clarification or want to make a suggestion.

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.

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

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

#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:

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

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

If day = 1, it will print:

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:

if (score >= 80)

But this works better with switch:

switch (choice)

6. Example with Characters

switch also works with char.

#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.

#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.

More from this blog

D

Dr. Ehoneah Obed

78 posts

Software engineer writing about systems: in code, in learning, in life. I reverse-engineer complex problems into frameworks. Pharmacist → SWE → Founder.