So far, every program in this series has executed its instructions in a straight line, one statement after another, without ever pausing to make a decision. Real programs rarely work this way. A login system needs to decide whether a password is correct, an e-commerce website needs to decide whether an item is in stock, and a grading program needs to decide which letter grade a score deserves. This kind of decision-making is handled through conditional statements.
Conditional statements allow a program to choose between different paths of execution based on whether a particular condition evaluates to true or false. Instead of following the same sequence of instructions every single time, the program can branch, skip certain sections entirely, or repeat different logic depending on the data it encounters while running.
In this tutorial, you will learn how the basic if statement works, how if-else extends it to handle two possible outcomes, how nested if statements and the else-if ladder handle multiple conditions, how the switch statement offers an alternative approach for specific kinds of comparisons, and how to avoid some of the most common mistakes beginners make when writing conditional logic.
The if statement is the simplest form of decision-making in C. It evaluates a condition, and if that condition turns out to be true, the block of code inside the if statement runs. If the condition is false, that block is skipped entirely, and the program continues with whatever comes after it.
if (condition) {
// code that runs only if condition is true
}
#include <stdio.h>
int main() {
int subscribers = 12000;
if (subscribers > 10000) {
printf("Channel has crossed 10K subscribers!");
}
return 0;
}
Channel has crossed 10K subscribers!
Since the condition inside the parentheses evaluates to true in this example, the message inside the curly braces is displayed. If the subscriber count had been lower than 10000, the program would have produced no output at all, since there is nothing to handle the false case yet.
While a plain if statement only handles the true case, most real decisions involve two possible outcomes. The if-else statement adds an alternate block of code that runs specifically when the condition turns out to be false.
if (condition) {
// runs if condition is true
} else {
// runs if condition is false
}
#include <stdio.h>
int main() {
int viewCount = 800;
if (viewCount >= 1000) {
printf("This video is performing well.");
} else {
printf("This video needs more promotion.");
}
return 0;
}
This video needs more promotion.
Because the view count in this example falls below the threshold set in the condition, the else block executes instead, demonstrating how if-else guarantees that exactly one of the two blocks will always run.
Sometimes a decision depends on more than one factor, where a second condition only needs to be checked after the first one has already been confirmed true. This is achieved by placing one if statement inside another, known as nesting.
#include <stdio.h>
int main() {
int subscribers = 15000;
int uploadsThisMonth = 6;
if (subscribers > 10000) {
if (uploadsThisMonth >= 4) {
printf("Channel qualifies for the creator program.");
} else {
printf("Subscriber count is sufficient, but uploads are too low.");
}
} else {
printf("Channel does not meet the subscriber requirement.");
}
return 0;
}
Channel qualifies for the creator program.
In this example, the inner condition checking upload frequency is only evaluated after the outer condition regarding subscriber count has already passed, illustrating how nested conditions allow a program to check requirements in a specific, dependent order.
When a program needs to check several distinct conditions in sequence, rather than just one or two, the else-if ladder provides a cleaner alternative to writing multiple separate nested if statements. Each condition is checked in order, and the first one that evaluates to true has its block executed, after which the remaining conditions are skipped entirely.
if (condition1) {
// runs if condition1 is true
} else if (condition2) {
// runs if condition1 is false and condition2 is true
} else {
// runs if none of the above conditions are true
}
#include <stdio.h>
int main() {
int score = 78;
if (score >= 90) {
printf("Grade: A");
} else if (score >= 75) {
printf("Grade: B");
} else if (score >= 60) {
printf("Grade: C");
} else {
printf("Grade: F");
}
return 0;
}
Grade: B
Even though the score also satisfies the third condition checking for 60 or above, the ladder stops as soon as it finds the first true condition, which in this case is the check for 75 or above, meaning only Grade B is displayed.
The switch statement offers an alternative way to handle multiple possible values of a single variable, particularly when comparing that variable against several fixed, specific values rather than ranges. It is often considered more readable than a long else-if ladder in these particular situations.
switch (expression) {
case value1:
// code for value1
break;
case value2:
// code for value2
break;
default:
// code if no case matches
}
#include <stdio.h>
int main() {
int dayNumber = 3;
switch (dayNumber) {
case 1:
printf("Monday upload scheduled.");
break;
case 2:
printf("Tuesday upload scheduled.");
break;
case 3:
printf("Wednesday upload scheduled.");
break;
default:
printf("No upload scheduled today.");
}
return 0;
}
Wednesday upload scheduled.
The break statement plays a critical role here, since it stops execution from continuing into the next case once a match has been found. Without it, the program would keep executing every subsequent case block until it either hits a break or reaches the end of the switch statement entirely, a behavior known as fall-through.
| Aspect | if-else Ladder | switch Statement |
|---|---|---|
| Best Suited For | Range-based conditions or comparisons involving multiple different variables. | Comparing a single variable against several fixed, specific values. |
| Readability | Can become harder to read as the number of conditions grows. | Often more readable when checking many possible fixed values. |
| Flexibility | Supports complex conditions involving relational and logical operators. | Limited to checking exact matches, typically with integers or characters. |
Conditional statements often become more powerful when combined with the logical operators covered in an earlier tutorial, allowing a single condition to check multiple requirements simultaneously rather than relying purely on nested if statements.
#include <stdio.h>
int main() {
int age = 20;
int hasID = 1;
if (age >= 18 && hasID == 1) {
printf("Access granted.");
} else {
printf("Access denied.");
}
return 0;
}
Access granted.
Combining conditions this way often results in cleaner, more compact code compared to writing separate nested if statements for each individual requirement.
| Mistake | Correct Practice |
|---|---|
| Using a single equals sign instead of a double equals sign inside a condition. | Always use the double equals sign when comparing values for equality inside a condition. |
| Forgetting the break statement inside switch cases. | Include a break statement at the end of each case, unless fall-through behavior is intentionally desired. |
| Writing overly deep nested if statements that are hard to follow. | Consider restructuring the logic using an else-if ladder or combining conditions with logical operators. |
| Assuming an else-if ladder checks every condition regardless of earlier matches. | Remember that the ladder stops at the first true condition and skips the remaining checks entirely. |
| Using switch with data types it does not support, such as floating-point values. | Use switch only with integer or character expressions, and rely on if-else for other data types. |
Conditional statements give C programs the ability to make decisions, transforming a rigid sequence of instructions into flexible logic that responds differently depending on the data it encounters. The if statement handles the simplest case, if-else extends this to cover two possible outcomes, nested if statements and the else-if ladder manage more complex decisions involving multiple conditions, and the switch statement offers a clean alternative when comparing a single variable against several fixed values.
In this tutorial, you learned how each of these constructs works individually, saw how they can be combined with logical operators for more expressive conditions, and reviewed some of the common mistakes that trip up beginners when writing decision-making logic. With a solid grasp of conditional statements in place, you are now ready to explore loops, which allow a program to repeat a block of code multiple times based on a condition.