-
Hajipur, Bihar, 844101
In programming, decision-making is a core concept. The if...else statement in C++ allows your program to execute certain parts of code depending on whether a condition is true or false.
You can think of it as giving your program a choice — “if this is true, do that; otherwise, do something else.”
The if statement checks a Boolean expression, and if it’s true, the block inside if runs. If it’s false, the block inside else runs (if provided).
The general syntax looks like this:
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
The condition is usually a comparison or logical expression.
The if block runs only when the condition is true.
The else block runs when the condition is false.
#include <iostream>
using namespace std;
int main() {
int age = 18;
if (age >= 18) {
cout << "You are eligible to vote.";
} else {
cout << "You are not eligible to vote.";
}
return 0;
}
Output:
You are eligible to vote.
Here, the condition (age >= 18) is true, so the program executes the first block.
Sometimes you only need to execute something when a condition is true. In that case, you can use if without else.
Example:
int marks = 85;
if (marks > 80) {
cout << "Excellent performance!";
}
If marks is not greater than 80, the program simply skips the if block and moves on.
When there are multiple conditions to check, you can use an if...else if...else structure.
C++ checks each condition in order until one is true.
Example:
#include <iostream>
using namespace std;
int main() {
int marks;
cout << "Enter your marks: ";
cin >> marks;
if (marks >= 90) {
cout << "Grade: A+";
} else if (marks >= 75) {
cout << "Grade: A";
} else if (marks >= 60) {
cout << "Grade: B";
} else if (marks >= 40) {
cout << "Grade: C";
} else {
cout << "Grade: F";
}
return 0;
}
Output Example:
Enter your marks: 82
Grade: A
This structure checks one condition after another until it finds a true one, then stops.
An if statement inside another if is called a nested if.
It’s useful when you need to test multiple conditions together.
Example:
#include <iostream>
using namespace std;
int main() {
int age = 25;
bool hasLicense = true;
if (age >= 18) {
if (hasLicense) {
cout << "You can drive.";
} else {
cout << "You must have a license to drive.";
}
} else {
cout << "You are too young to drive.";
}
return 0;
}
Output:
You can drive.
Here, the second condition (hasLicense) is only checked if the first (age >= 18) is true.
You can combine multiple conditions using logical operators like && (AND), || (OR), and ! (NOT).
Example using AND (&&):
int age = 20;
bool hasID = true;
if (age >= 18 && hasID) {
cout << "Access granted.";
} else {
cout << "Access denied.";
}
Both conditions must be true for the message “Access granted” to display.
Example using OR (||):
bool weekend = true;
bool holiday = false;
if (weekend || holiday) {
cout << "You can relax today.";
} else {
cout << "It’s a workday.";
}
If either condition is true, the statement inside if executes.
You can use comparison operators (==, !=, <, >, <=, >=) inside if conditions.
Example:
int a = 10, b = 20;
if (a == b)
cout << "Both numbers are equal.";
else if (a > b)
cout << "a is greater.";
else
cout << "b is greater.";
Output:
b is greater.
Here’s a classic example of using if...else for checking whether a number is even or odd.
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num % 2 == 0)
cout << num << " is even.";
else
cout << num << " is odd.";
return 0;
}
Output Example:
Enter a number: 5
5 is odd.
C++ provides a shorthand version of if...else using the ternary operator ?:.
Syntax:
condition ? expression_if_true : expression_if_false;
Example:
int num = 10;
cout << (num % 2 == 0 ? "Even" : "Odd");
Output:
Even
The ternary operator is commonly used for short conditional expressions.
If you have multiple statements to execute within if or else, use braces {} to define the block.
Without braces, only the first statement will be considered part of that block.
Example:
if (true)
cout << "Hello ";
cout << "World!";
Output:
Hello World!
Here, World! is printed regardless of the condition because it’s not inside the if block.
Always use braces {} for clarity.
The if...else statement in C++ is used to control the flow of your program based on conditions.
You can use simple if, if...else, or more advanced structures like else if ladders and nested conditions. Logical operators help combine multiple checks efficiently, while the ternary operator offers a compact alternative for simple decisions.
Mastering these conditions allows your program to make intelligent choices and behave differently in various situations.
What is the purpose of using an if...else statement in C++?
What happens if you write an if statement without braces {} for multiple lines of code?
Explain the difference between if and else if.
How does a nested if statement work?
Write a program to check if a number is positive, negative, or zero.
Write a program to find the greatest of three numbers using if...else if.
Write a program to determine if a person is eligible to donate blood (age ≥ 18 and weight ≥ 50).
What is the output of this code?
int x = 5;
if (x = 0)
cout << "True";
else
cout << "False";
Write a program using a ternary operator to check whether a number is even or odd.
What is the importance of using logical operators in if conditions?