In real life, we make decisions every day based on certain conditions. For example, "If it is raining, I will take an umbrella; otherwise, I will wear sunglasses." Programming is no different!
In C programming, conditional statements allow your code to make decisions and execute different blocks of code based on whether a specific condition is true or false. This is known as control flow.
Mastering the if...else statements is critical for any C programmer, as it forms the backbone of logic in almost every software application.
if StatementThe if statement is the simplest form of decision-making in C. It tells the compiler to execute a specific block of code only if a given condition evaluates to true (a non-zero value).
Syntax:
if (condition) {
// Code to be executed if the condition is true
}
If the condition is false (evaluates to 0), the code inside the curly braces {} is completely ignored, and the program continues with the next line of code after the if block.
if Example#include <stdio.h>int main() { int age = 20;
// Checking if age is greater than or equal to 18 if (age >= 18) { printf("You are eligible to vote!\n"); }
printf("Program continues...\n"); return 0; }
if...else StatementWhat if you want to execute one block of code when the condition is true, and a different block of code when it is false? This is where the else statement comes in.
Syntax:
if (condition) {
// Code to run if condition is true
} else {
// Code to run if condition is false
}
if...else Example#include <stdio.h>int main() { int time = 20;
if (time < 18) { printf("Good day.\n"); } else { printf("Good evening.\n"); }
return 0; }
In the example above, since time is 20, the condition time < 18 is false. Therefore, the code inside the else block runs, printing "Good evening."
if...else if...else LadderOften, you will need to test multiple conditions. The else if statement allows you to chain together multiple checks. The program will evaluate them one by one from top to bottom. As soon as a condition is true, its corresponding code block is executed, and the rest of the ladder is skipped.
Syntax:
if (condition1) {
// Code for condition1
} else if (condition2) {
// Code for condition2
} else if (condition3) {
// Code for condition3
} else {
// Code if none of the above are true
}
#include <stdio.h>int main() { int score = 85;
if (score >= 90) { printf("Grade: A\n"); } else if (score >= 80) { printf("Grade: B\n"); } else if (score >= 70) { printf("Grade: C\n"); } else if (score >= 60) { printf("Grade: D\n"); } else { printf("Grade: F\n"); }
return 0; }
if...else StatementsYou can place an if or if...else statement inside another if or else statement. This is called nesting. It is useful when you need to test for a condition, and based on that result, test for another condition.
if Example#include <stdio.h>int main() { int num = 35;
if (num > 0) { printf("Number is positive.\n"); // Nested if if (num % 2 == 0) { printf("Number is also even.\n"); } else { printf("Number is also odd.\n"); } } else { printf("Number is negative or zero.\n"); }
return 0; }
Pro Tip: While nesting is powerful, nesting too deep (e.g., more than 3 levels) can make your code very difficult to read. Try to use logical operators (
&&,||) to combine conditions when possible!
If you have a simple if...else statement that assigns a value to a variable, you can use the ternary operator ? : as a shorthand. This makes your code more concise and readable.
Syntax:
variable = (condition) ? expressionTrue : expressionFalse;
#include <stdio.h>int main() { int time = 20;
// Using Ternary Operator (time < 18) ? printf("Good day.\n") : printf("Good evening.\n");
return 0; }
When writing conditions in C, beginners often fall into a few common traps. Here is how to avoid them:
= vs ==)In C, a single equals sign = is an assignment operator, while a double equals == is an equality operator.
Bad: if (x = 10) (This assigns 10 to x, evaluates to true, and creates a bug).
Good: if (x == 10) (This correctly checks if x is equal to 10).
If you omit curly braces {}, only the very next line is considered part of the if statement. It is a best practice to always use curly braces, even for single lines, to prevent future bugs.
Which operator is used for equality comparison in an `if` statement?
What will `if (0)` evaluate to in C?