We use switch and if to make decisions in code. They help the program choose what to do based on different conditions.
When you have many choices based on one variable's value.
When you want clearer code for multiple fixed options.
When you need to check ranges or complex conditions, if is better.
When you want faster decision making for many fixed cases, switch can be faster.
When you want to handle different commands or menu options.
Syntax
C
switch (variable) {
case value1:
// code to runbreak;
case value2:
// code to runbreak;
default:
// code if no case matchesbreak;
}
// vsif (condition1) {
// code to run
} elseif (condition2) {
// code to run
} else {
// code if no condition matches
}
switch works only with fixed values like numbers or characters.
if can check any condition, like ranges or multiple variables.
Examples
This switch prints the day name for 1 or 2, otherwise prints "Other day".
Hint: Remember switch(variable) with braces and case labels [OK]
Common Mistakes:
Omitting parentheses around variable
Using if inside switch incorrectly
Missing break causing fall-through
3. What will be the output of this C code?
int x = 2;
switch (x) {
case 1: printf("One\n"); break;
case 2: printf("Two\n");
case 3: printf("Three\n"); break;
default: printf("Default\n");
}
medium
A. Two
B. Two
Three
C. Three
D. Default
Solution
Step 1: Identify matching case
The variable x is 2, so case 2 matches and prints "Two".
Step 2: Check for break after case 2
There is no break after case 2, so execution falls through to case 3 and prints "Three".
Final Answer:
Two
Three -> Option B
Quick Check:
Missing break causes fall-through [OK]
Hint: No break after case causes next case to run too [OK]
Common Mistakes:
Assuming break after every case automatically
Ignoring fall-through behavior
Thinking default runs if case matches
4. Find the error in this code snippet:
int num = 3;
switch (num) {
case 1:
printf("One\n");
case 2:
printf("Two\n");
break;
default:
printf("Default\n");
}
medium
A. Case labels must be strings, not numbers.
B. Switch variable must be a character, not int.
C. Missing break after case 1 causes fall-through.
D. Default case must be last, but it is not.
Solution
Step 1: Check breaks after cases
Case 1 lacks a break, so if num is 1, it will fall through to case 2 and print both lines.
Step 2: Validate switch rules
Switch can use int variables, default can be anywhere, and case labels are numbers here, which is correct.
Final Answer:
Missing break after case 1 causes fall-through. -> Option C
Quick Check:
Always add break to avoid fall-through [OK]
Hint: Add break after each case to prevent fall-through [OK]
Common Mistakes:
Thinking switch only works with chars
Believing default must be last
Confusing case label types
5. You want to print a message based on a score variable: - "Fail" if score < 50 - "Pass" if score is between 50 and 75 inclusive - "Excellent" if score > 75
Which approach is best in C?
hard
A. Use a switch with default only and if inside cases.
B. Use a switch statement with cases for each score range.
C. Use multiple switch statements for each condition.
D. Use if-else statements to check score ranges.
Solution
Step 1: Analyze score conditions
The conditions involve ranges (less than 50, between 50 and 75, greater than 75).
Step 2: Choose suitable control structure
Switch works only with fixed values, not ranges, so if-else is better for ranges.
Final Answer:
Use if-else statements to check score ranges. -> Option D
Quick Check:
Ranges need if-else, not switch [OK]
Hint: Use if for ranges, switch for fixed values only [OK]
Common Mistakes:
Trying to use switch for ranges
Using multiple switches unnecessarily
Confusing default case with range checks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.