Welcome to The Coding College! In this tutorial, we’ll explore the if…else statement in C, one of the most powerful tools for decision-making in programming. With practical examples and step-by-step explanations, you’ll master how to control the flow of your programs based on conditions.
What is an If…Else Statement?
The if…else statement in C is used to execute a block of code only when a specified condition is true. If the condition is false, the program can execute an alternate block of code using the else clause.
Basic Syntax:
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
condition
: An expression that evaluates to eithertrue
(non-zero) orfalse
(0
).- The
if
block is executed when the condition istrue
. - The
else
block is executed when the condition isfalse
.
Simple If…Else Example
#include <stdio.h>
int main() {
int age = 20;
if (age >= 18) {
printf("You are an adult.\n");
} else {
printf("You are not an adult.\n");
}
return 0;
}
Output:
You are an adult.
Explanation:
- The condition
age >= 18
is true, so theif
block is executed. - If
age
were less than 18, theelse
block would execute.
Nested If…Else
You can nest if...else
statements for more complex decision-making.
Example:
#include <stdio.h>
int main() {
int marks = 85;
if (marks >= 90) {
printf("Grade: A\n");
} else if (marks >= 75) {
printf("Grade: B\n");
} else if (marks >= 50) {
printf("Grade: C\n");
} else {
printf("Grade: F\n");
}
return 0;
}
Output:
Grade: B
Explanation:
The program checks multiple conditions in sequence. When it finds the first true
condition (marks >= 75
), it executes the corresponding block and skips the rest.
Using Logical Operators with If…Else
You can combine multiple conditions using logical operators:
&&
: Logical AND||
: Logical OR!
: Logical NOT
Example:
#include <stdio.h>
int main() {
int age = 25;
int hasLicense = 1; // 1 = true, 0 = false
if (age >= 18 && hasLicense) {
printf("You can drive.\n");
} else {
printf("You cannot drive.\n");
}
return 0;
}
Output:
You can drive.
Inline If…Else with the Ternary Operator
C also supports a shorthand version of if...else
called the ternary operator.
Syntax:
(condition) ? expression_if_true : expression_if_false;
Example:
#include <stdio.h>
int main() {
int age = 16;
(age >= 18) ? printf("You are an adult.\n") : printf("You are not an adult.\n");
return 0;
}
Output:
You are not an adult.
Common Mistakes in If…Else
1. Missing Braces {}
If there’s only one statement in the block, braces are optional, but omitting them in multi-statement blocks causes logical errors.
Incorrect:
if (1 > 0)
printf("True\n");
printf("False\n"); // This always runs!
Correct:
if (1 > 0) {
printf("True\n");
printf("False\n"); // Part of the if block
}
2. Using Assignment Instead of Equality
Incorrect:
if (x = 10) { // Single = assigns, doesn't compare
printf("This is always true.\n");
}
Correct:
if (x == 10) { // Double == compares values
printf("This is correct.\n");
}
Practical Applications
Example: Check Even or Odd Number
#include <stdio.h>
int main() {
int num = 4;
if (num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}
return 0;
}
Output:
4 is even.
Example: Find the Largest Number
#include <stdio.h>
int main() {
int a = 10, b = 20;
if (a > b) {
printf("%d is larger.\n", a);
} else {
printf("%d is larger.\n", b);
}
return 0;
}
Output:
20 is larger.
Key Takeaways
- Basic Usage: Use
if...else
to control program flow based on conditions. - Logical Operators: Combine conditions with
&&
,||
, and!
. - Nested Conditions: Use nested
if...else
for complex decisions. - Ternary Operator: Use
(condition) ? true : false
for concise conditions. - Error Avoidance: Always use
==
for comparison and add braces{}
for clarity.