C-Program
Control Statements
Last Updated on: 23rd Jan 2025 11:15:57 AM
If-Else
The if-else
statement in C is a core control flow mechanism that lets your program make decisions. It executes different sets of code depending on whether a condition is true or false. Let's break it down simply and in detail:
1. The if
Statement (Basic Form)
The simplest form checks a single condition:
if (condition) {
// Code to execute if the condition is true
}
if (condition)
: Thecondition
is an expression that evaluates to either true (any non-zero value) or false (zero). It's enclosed in parentheses()
.{ ... }
(Code Block): The code inside the curly braces{}
is the body of theif
statement. This code runs only if thecondition
is true.
Example:
#include <stdio.h>
int main() {
int temperature = 25;
if (temperature > 20) {
printf("It's a warm day!\n");
}
return 0;
}
Here, temperature > 20
is true (25 is greater than 20), so "It's a warm day!" is printed. If temperature
was 15, nothing would be printed.
2. The else
Statement (Adding an Alternative):
The else
keyword provides a block of code to execute when the if
condition is false:
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
Example:
#include <stdio.h>
int main() {
int temperature = 10;
if (temperature > 20) {
printf("It's a warm day!\n");
} else {
printf("It's a cold day!\n");
}
return 0;
}
Now, because temperature
is 10 (not greater than 20), the else
block executes, printing "It's a cold day!"
3. The else if
Statement (Checking Multiple Conditions):
else if
lets you check multiple conditions sequentially:
if (condition1) {
// Code if condition1 is true
} else if (condition2) {
// Code if condition1 is false AND condition2 is true
} else if (condition3) {
// Code if condition1 and condition2 are false AND condition3 is true
} else {
// Code if ALL conditions are false
}
Example (Grading System):
#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 {
printf("Grade: D or below\n");
}
return 0;
}
Here, the conditions are checked in order. score >= 80
is the first true condition, so "Grade: B" is printed, and the rest are skipped.
4. Nested if
Statements (Conditions within Conditions):
You can put if
statements inside other if
or else
blocks:
if (outer_condition) {
if (inner_condition) {
// Code if BOTH outer_condition AND inner_condition are true
} else {
// Code if outer_condition is true BUT inner_condition is false
}
}
5. Key Points and Best Practices
- Curly Braces
{}
: Even if a block has only one statement, always use curly braces. This makes your code clearer and prevents errors if you add more statements later. - Comparison Operators: Conditions use operators like:
==
(equal to)!=
(not equal to)>
(greater than)<
(less than)>=
(greater than or equal to)<=
(less than or equal to)
- Logical Operators: Combine conditions with:
&&
(AND): Both conditions must be true.||
(OR): At least one condition must be true.!
(NOT): Inverts a condition's truth value.
- Indentation: Indent the code inside
if
,else if
, andelse
blocks. This greatly improves readability.
#include <stdio.h>
int main() {
int age = 25;
char hasLicense = 'Y'; // 'Y' for yes, 'N' for no
if (age >= 18 && hasLicense == 'Y') {
printf("You are eligible to drive.\n");
} else {
printf("You are not eligible to drive.\n");
}
return 0;
}
This checks if the person is at least 18 and has a license.
The if-else
statement is fundamental for creating programs that can handle different situations and make decisions based on data. Understanding it well is crucial for C programming..