switch compares an integer or character with several case values.
- Each case should end with
break, or execution falls through to the next case. defaultruns when no case matches.
Choose between many cases.
✍️ 2 exercises in this lessonswitch compares an integer or character with several case values.
break, or execution falls through to the next case.default runs when no case matches.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.
#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;
}
24
Check what you learned in “C Switch”.
Stop after a matching case.
case 1:
printf("One");
;
What happens without break in a case?
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