Chapter 3: Mastering Conditional Statements
In the previous chapter, we explored how to store data. However, a program that only performs math in a straight line is not very useful. Real-world applications—from video games to banking software—depend on the program's ability to make decisions based on user input or data calculations. In C, we handle this logic using Conditional Statements.
The If-Else Structure
The if statement is the most fundamental building block of logic in C. It checks if a condition (an expression) is "True" (non-zero) or "False" (zero). If the condition is met, the code inside the block executes. If not, the program skips that block.
if (age >= 18) {
printf("You are an adult.\n");
} else {
printf("You are a minor.\n");
}
When you have multiple conditions, the else if structure is your best friend. It allows you to chain multiple tests together sequentially. Only the first block that evaluates to "True" will run, and the rest will be ignored.
The Switch Statement
While if-else chains are great for ranges, they become messy when you are checking one variable against many constant values (like menu options). The switch statement is a cleaner, more efficient alternative for this specific scenario.
int option = 2;
switch(option) {
case 1:
printf("Starting Game...\n");
break;
case 2:
printf("Loading Settings...\n");
break;
default:
printf("Invalid Option!\n");
}
Crucial Note: The break statement is mandatory! Without it, C will perform "fall-through," where it executes every case block following the matching one, regardless of whether their conditions are met.
Boolean Logic
C does not have a native "boolean" type in older standards (though <stdbool.h> is available now). C considers 0 to be False and anything else to be True. This allows for clever shortcuts, but beginners often get tripped up using = (assignment) instead of == (comparison) inside an if statement, which leads to silent bugs where the condition always evaluates to True.
Common Pitfalls
- The Semicolon Trap: Placing a semicolon after an
ifstatement (e.g.,if (x > 0);) will cause the block to be ignored entirely! - Forgetting Break: Omitting a break in a switch statement is one of the most common causes of unexpected program behavior.
- Nested Complexity: Deeply nesting
ifstatements inside each other makes code unreadable. Try to keep logic flat.
Try It Yourself
- Write a program that takes a student's marks as input and prints their grade (A, B, C, or F) using
if-else if. - Create a simple calculator menu using a
switchstatement that asks the user to choose addition, subtraction, or multiplication.
The switch Statement
When you're checking one variable against many possible values, a switch statement is often cleaner than a long chain of if/else if blocks.
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");
}
Don't forget the break statement after each case — without it, execution "falls through" into the next case, which is a common source of bugs for beginners.
CodeMaster Academy