C Switch

Choose between many cases.

✍️ 2 exercises in this lesson

switch compares an integer or character with several case values.

  • Each case should end with break, or execution falls through to the next case.
  • default runs when no case matches.

Key Takeaways: C Switch

Mastering C Switch helps you write clean and reliable C code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

C Switch Code Example

C
#include <stdio.h>

int main() {
    char op = '*';
    int a = 6, b = 4;

    switch (op) {
        case '+': printf("%d\n", a + b); break;
        case '-': printf("%d\n", a - b); break;
        case '*': printf("%d\n", a * b); break;
        case '/': printf("%d\n", a / b); break;
        default:  printf("Unknown operator\n");
    }
    return 0;
}
Output
24
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

C Switch Practice Exercises

Check what you learned in “C Switch”.

/ 2 solved
Fill in the blank

Stop after a matching case.

case 1:
  printf("One");
  ;
✓ Correct! Not quite. Try again.
Multiple choice

What happens without break in a case?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services