The 'if' Statement
Branching logic is fundamental to controlling program execution flow. In C, the if statement allows code to execute specific blocks based on whether a condition evaluates to true or false. C interprets any non-zero value as true, while zero represents false.
Single and Multi-Branch Structures
A basic branch involves one condition. If the condition is met, one block runs; otherwise, an optional else block runs.
#include <stdio.h>
int main() {
int balance = 0;
scanf("%d", &balance);
if (balance > 1000000) {
printf("Financial freedom achieved.\n");
} else {
printf("Keep working hard.\n");
}
return 0;
}For multiple distinct conditions, else if provides a ladder of checks.
#include <stdio.h>
int main() {
int score;
scanf("%d", &score);
if (score >= 90) {
printf("Grade: A\n");
} else if (score >= 80) {
printf("Grade: B\n");
} else if (score >= 70) {
printf("Grade: C\n");
} else {
printf("Grade: F\n");
}
return 0;
}The Dangling Else Problem
Nested if statements can introduce ambiguity known as the dangling else problem. The else clause always matches the closest unmatched if.
#include <stdio.h>
int main() {
int x = 0, y = 5;
if (x == 1)
if (y == 5)
printf("Condition met\n");
else
printf("This else belongs to the inner if(y)\n");
return 0;
}To enforce specific logic and improve readability, braces {} should be used to explicitly define blocks.
#include <stdio.h>
int main() {
int x = 0, y = 5;
if (x == 1) {
if (y == 5) {
printf("Condition met\n");
}
} else {
printf("This else now belongs to the outer if(x)\n");
}
return 0;
}The 'switch' Statement
When handling multiple potential values for a single variable, a switch statement often provides cleaner syntax and better performance than a long if-else if chain. It jumps directly to the matching case label.
Structure and Syntax
The switch expression must evaluate to an integer type. Each case label must be an integer constant.
#include <stdio.h>
int main() {
int day;
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 input\n");
break;
}
return 0;
}The Importance of 'break'
The break keyword terminates the switch execution. If omitted, the program continues executing subsequent cases regardless of their labels, a behavior known as fall-through.
#include <stdio.h>
int main() {
int day = 1;
switch (day) {
case 1:
printf("Monday\n");
/* No break here, falls through */
case 2:
printf("Tuesday\n");
break;
}
// Output: Monday \n Tuesday
return 0;
}However, fall-through can be utilized intentionally to group multiple cases to execute the same logic.
#include <stdio.h>
int main() {
int day;
scanf("%d", &day);
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5:
printf("Weekday\n");
break;
case 6:
case 7:
printf("Weekend\n");
break;
default:
printf("Unknown day\n");
}
return 0;
}The 'default' Case
The default label handles values that do not match any defined case. While not strictly required by the compiler, it is best practice to include it for error handling or catching unexpected inputs.