Decisions with if and switch
Choose between paths with if, else if, else and switch.
An if statement checks a condition in round brackets. If the condition is true (any non-zero value), the block in curly braces runs.
int main(void) {
int battery = 15;
if (battery < 20) {
printf("Battery low: %d%%\n", battery);
} else {
printf("Battery fine.\n");
}
return 0;
}
Battery low: 15%
else if chains
Conditions are tested from top to bottom, and only the first matching block runs.
int main(void) {
int hour = 14;
if (hour < 12) {
printf("Good morning\n");
} else if (hour < 17) {
printf("Good afternoon\n");
} else {
printf("Good evening\n");
}
return 0;
}
Good afternoon
Use && for "and", || for "or" and ! for "not" when you need to combine conditions.
switch
When you compare one value against many fixed options, switch can be clearer than a long else if chain. Each case needs a break, otherwise execution "falls through" into the next case.
int main(void) {
int day = 3;
switch (day) {
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n"); break;
default: printf("Another day\n");
}
return 0;
}
Wednesday
GATE-style practice questions
What does the following program print?
int main(void) {
int x = 3;
switch (x) {
case 1: printf("one ");
case 3: printf("three ");
case 4: printf("four ");
default: printf("other ");
case 5: printf("five ");
}
printf("\n");
return 0;
}
Show answer and explanation
Answer: CThere are no break statements, so after jumping to case 3 execution falls through every following label, including default and case 5, to the end of the switch.
What does the following program print?
int main(void) {
int a = 5;
if (a = 0)
printf("zero ");
else
printf("nonzero ");
printf("%d\n", a);
return 0;
}
Show answer and explanation
Answer: Ba = 0 is an assignment, not a comparison. It sets a to 0, and the value of the expression is 0, which is false, so the else branch runs. a is now 0.
What does the following program print?
int main(void) {
int i = 2, j = 0;
if (i > 1)
if (j > 0)
printf("A\n");
else
printf("B\n");
return 0;
}
Show answer and explanation
Answer: BThis is the "dangling else": an else always pairs with the nearest unmatched if, regardless of indentation. So the else belongs to if (j > 0). i > 1 is true and j > 0 is false, so it prints B.