A nested if statement means writing one if statement inside another if statement. It is useful when a second condition needs to be checked only after the first condition is true.
When an if statement is placed inside another if statement, it is called a nested if.
if (condition1) {
if (condition2) {
// statements
}
}
The inner condition is checked only when the outer condition is true.
The general syntax of a nested if statement is:
if (condition1) {
// Outer if
if (condition2) {
// Inner if
}
}
int age = 20;
if (age >= 18) {
if (age <= 60) {
std::cout << "Adult";
}
}
First, the program checks whether the age is at least 18. If that is true, it checks whether the age is 60 or below.
A nested if statement works in two stages:
int marks = 80;
if (marks >= 40) {
if (marks >= 75) {
std::cout << "Distinction";
}
}
The inner condition is checked only because the student has passed.
int age = 25;
if (age >= 18) {
if (age < 60) {
std::cout << "Adult";
}
}
Both conditions must be true for the message to be displayed.
int age = 15;
if (age >= 18) {
if (age < 60) {
std::cout << "Adult";
}
}
Here the outer condition is false, so the inner if statement is never checked.
int age = 65;
if (age >= 18) {
if (age < 60) {
std::cout << "Adult";
}
}
The outer condition is true, but the inner condition is false. Therefore, the message is not displayed.
bool loggedIn = true;
bool isAdmin = true;
if (loggedIn) {
if (isAdmin) {
std::cout << "Admin access";
}
}
The user must be logged in before the program checks whether the user is an administrator.
#include <iostream>
#include <string>
int main() {
std::string username = "admin";
std::string password = "1234";
if (username == "admin") {
if (password == "1234") {
std::cout << "Login successful";
}
}
return 0;
}
An inner if can also have its own else block.
int age = 20;
if (age >= 18) {
if (age >= 21) {
std::cout << "Age is 21 or above";
}
else {
std::cout << "Age is between 18 and 20";
}
}
int marks = 85;
if (marks >= 40) {
if (marks >= 75) {
std::cout << "Good result";
}
else {
std::cout << "Passed";
}
}
else {
std::cout << "Failed";
}
int age = 25;
bool hasID = true;
if (age >= 18) {
if (hasID && age <= 60) {
std::cout << "Access allowed";
}
}
Logical operators can also be used inside the nested condition.
int marks = 70;
int attendance = 80;
if (marks >= 40) {
if (attendance >= 75) {
std::cout << "Eligible for examination";
}
}
The student must satisfy both the marks and attendance requirements.
int age = 20;
bool hasLicense = true;
if (age >= 18) {
if (hasLicense) {
std::cout << "Can drive";
}
}
#include <iostream>
int main() {
int age;
std::cout << "Enter your age: ";
std::cin >> age;
if (age >= 18) {
if (age <= 60) {
std::cout << "You are an adult";
}
}
return 0;
}
int number = 20;
if (number > 0) {
if (number % 2 == 0) {
std::cout << "Positive even number";
}
}
The program first checks whether the number is positive and then checks whether it is even.
int number = 15;
if (number > 0) {
if (number % 2 == 0) {
std::cout << "Positive even number";
}
else {
std::cout << "Positive odd number";
}
}
else {
std::cout << "Number is not positive";
}
#include <iostream>
#include <string>
int main() {
std::string username;
std::string password;
std::cout << "Username: ";
std::cin >> username;
std::cout << "Password: ";
std::cin >> password;
if (username == "admin") {
if (password == "1234") {
std::cout << "Login successful";
}
else {
std::cout << "Wrong password";
}
}
else {
std::cout << "Wrong username";
}
return 0;
}
double amount = 6000;
bool member = true;
if (amount >= 5000) {
if (member) {
std::cout << "Member discount available";
}
}
The customer must meet the purchase requirement before membership is checked.
An if statement can contain another if, and that inner if can contain another if. This creates multiple levels of nesting.
bool loggedIn = true;
bool verified = true;
bool isAdmin = true;
if (loggedIn) {
if (verified) {
if (isAdmin) {
std::cout << "Admin access";
}
}
}
Although possible, too many nesting levels can make code harder to read.
char grade = 'A';
if (grade == 'A') {
if (grade == 'A') {
std::cout << "Excellent grade";
}
}
The example demonstrates the structure of a nested condition. In real programs, redundant conditions like this should be avoided.
int marks = 82;
int attendance = 80;
if (marks >= 40) {
if (attendance >= 75) {
if (marks >= 75) {
std::cout << "Passed with good marks";
}
else {
std::cout << "Passed";
}
}
else {
std::cout << "Attendance is too low";
}
}
else {
std::cout << "Failed";
}
Sometimes a nested if can be simplified by using the logical && operator.
Nested if:
if (age >= 18) {
if (hasID) {
std::cout << "Allowed";
}
}
Using AND:
if (age >= 18 && hasID) {
std::cout << "Allowed";
}
Both approaches can represent the same simple requirement. Choose the clearer structure for the situation.
An inner condition can also contain an else-if ladder.
int marks = 85;
if (marks >= 40) {
if (marks >= 90) {
std::cout << "Grade A";
}
else if (marks >= 75) {
std::cout << "Grade B";
}
else {
std::cout << "Grade C";
}
}
else {
std::cout << "Fail";
}
Good indentation makes nested conditions much easier to understand.
#include <iostream>
int main() {
int marks;
int age;
std::cout << "Enter age: ";
std::cin >> age;
std::cout << "Enter marks: ";
std::cin >> marks;
if (age >= 18) {
if (marks >= 40) {
std::cout << "Eligible for admission";
}
else {
std::cout << "Marks are too low";
}
}
else {
std::cout << "Age is below the required level";
}
return 0;
}
#include <iostream>
int main() {
bool loggedIn = true;
bool accountActive = true;
double balance = 5000;
if (loggedIn) {
if (accountActive) {
if (balance > 0) {
std::cout << "Account is ready";
}
else {
std::cout << "Insufficient balance";
}
}
else {
std::cout << "Account is inactive";
}
}
else {
std::cout << "Please login";
}
return 0;
}
if (condition1) {
if (condition2) {
// clear and readable code
}
}
| Concept | Meaning |
|---|---|
| Outer if | The first condition that controls access to the inner if. |
| Inner if | A condition placed inside another if statement. |
| Nested if-else | An inner if can have its own else block. |
| Nested levels | Multiple levels are possible, but excessive nesting can reduce readability. |
| Logical operators | Can sometimes simplify nested conditions. |
if (condition1) {
if (condition2) {
// statements
}
}
else {
// outer else
}
Question: When is the inner if statement checked in a nested if?