Back to C++
2026-01-096 min read

Nested if (C++)

Learn Nested if (C++) step by step with clear examples and exercises.

Why This Matters

Understanding Nested If Statements in C++ is crucial for writing complex conditional logic in your programs. They allow you to create multiple levels of nested conditions, making your code more flexible and versatile. Mastering this concept can help you tackle real-world programming problems, as well as prepare for job interviews and competitive coding challenges.

Prerequisites

Before diving into Nested If Statements, it's essential that you have a solid understanding of the following topics:

  1. Basic C++ syntax (variables, data types, operators)
  2. Control structures (if-else, loops)
  3. Functions and function declarations
  4. Standard input/output (cin, cout)
  5. Understanding of basic data structures like arrays and vectors
  6. Familiarity with the concept of scope in C++
  7. Comprehension of mathematical operators and logical operators
  8. Knowledge of conditional expressions and ternary operators

Core Concept

Nested If Statement Basics

A nested if statement is simply an if statement inside another if or else block. This allows you to create multiple levels of conditional logic within a single code block. Here's a simple example:

#include <iostream>
using namespace std;

int main() {
int number = 10;

// Outer if statement checks whether the number is greater than 5
if (number > 5) {
cout << "Number is greater than 5.\n";

// Inner if statement checks if the number is greater than 15
if (number > 15) {
cout << "Number is greater than 15.\n";
} else {
cout << "Number is between 6 and 15.\n";
}
} else {
cout << "Number is less than 5.\n";
}

return 0;
}

In this example, the outer if statement checks whether the number is greater than 5. If it is, the inner if statement checks if the number is greater than 15. If neither condition is true, the final else block executes, outputting "Number is less than 5."

Nested Else-If Statements

You can also use nested else-if statements to create a more organized and readable conditional structure. Here's an example:

#include <iostream>
using namespace std;

int main() {
int number = 10;

// Outer if statement checks whether the number is greater than 20
if (number > 20) {
cout << "Number is greater than 20.\n";
} else if (number > 15) {
cout << "Number is between 16 and 20.\n";
} else if (number > 10) {
cout << "Number is between 11 and 15.\n";
} else if (number > 5) {
cout << "Number is between 6 and 10.\n";
} else {
cout << "Number is less than 6.\n";
}

return 0;
}

In this example, the outer if statement checks whether the number is greater than 20. If it is not, the first nested else-if statement checks if the number is between 16 and 20. If neither of these conditions are true, the next nested else-if statement checks if the number is between 11 and 15, and so on.

Nested If Statements with Loops

Nested if statements can also be used in loops to create more complex conditional logic. Here's an example that calculates the sum of all even numbers between 1 and 100:

#include <iostream>
using namespace std;

int main() {
int sum = 0;
for (int i = 1; i <= 100; ++i) {
// Inner if statement checks if the current number is even
if (i % 2 == 0) {
sum += i;
}
}

cout << "Sum of even numbers between 1 and 100: " << sum << "\n";

return 0;
}

Worked Example

Let's create a program that prompts the user for two integers and checks whether the second integer is greater than, less than, or equal to the first one. If it's greater, print "The second number is greater."; if it's less, print "The second number is smaller."; and if they're equal, print "Both numbers are equal."

#include <iostream>
using namespace std;

int main() {
int num1, num2;

cout << "Enter the first integer: ";
cin >> num1;
cout << "Enter the second integer: ";
cin >> num2;

// Nested if statements to compare the two numbers
if (num1 > num2) {
cout << "The first number is greater.\n";
} else if (num1 < num2) {
cout << "The first number is smaller.\n";
} else {
cout << "Both numbers are equal.\n";
}

return 0;
}

Common Mistakes

  1. Forgetting to close the inner if or else-if statement with a corresponding brace (}).
  2. Using the wrong comparison operator (e.g., using = instead of == for equality checks).
  3. Not handling all possible cases, such as when both numbers are equal in nested if-else statements.
  4. Forgetting to initialize variables before using them in conditional statements.
  5. Using unnecessary nested levels when a simple else-if structure would suffice.

Practice Questions

Question 1

Write a program that prompts the user for two integers and checks whether the second integer is greater than, less than, or equal to the first one. If it's greater, print "The second number is greater."; if it's less, print "The second number is smaller."; and if they're equal, print "Both numbers are equal."

#include <iostream>
using namespace std;

int main() {
int num1, num2;

cout << "Enter the first integer: ";
cin >> num1;
cout << "Enter the second integer: ";
cin >> num2;

// Nested if statements to compare the two numbers
if (num1 > num2) {
cout << "The first number is greater.\n";
} else if (num1 < num2) {
cout << "The first number is smaller.\n";
} else {
cout << "Both numbers are equal.\n";
}

return 0;
}

Question 2

Write a program that asks the user to enter their age and checks whether they can drive a car, vote, or join the military based on their age. If they can do all three, print "You have full rights."; if they can only drive a car, print "You can drive a car."; if they can only vote, print "You can vote."; and if they can't do any of these things, print "Sorry, you are too young."

#include <iostream>
using namespace std;

int main() {
int age;

cout << "Enter your age: ";
cin >> age;

// Nested if statements to check the user's rights based on their age
if (age >= 16) {
if (age >= 18) {
if (age >= 21) {
cout << "You have full rights.\n";
} else {
cout << "You can vote.\n";
}
} else {
cout << "You can drive a car.\n";
}
} else {
cout << "Sorry, you are too young.\n";
}

return 0;
}

FAQ

Why do we use nested if statements?

Nested if statements allow us to create complex conditional logic within a single code block. They help make our code more flexible and versatile, allowing us to handle multiple conditions at different levels of depth.

What is the difference between an if statement and an else-if statement?

An if statement checks a condition and executes the associated code if the condition is true. An else-if statement checks a condition after an initial if statement has failed, and continues to check additional conditions until one is found to be true or all conditions have been exhausted.

What happens when we forget to close an if or else-if block with a brace?

If we forget to close an if or else-if block with a brace (}), the code following that block will be executed as part of the conditional block, potentially leading to unexpected results.

What is the correct way to handle multiple conditions in C++?

In C++, you can use either nested if statements or nested else-if statements to handle multiple conditions. The choice between the two depends on the specific requirements of your program and the readability of the resulting code.

Nested if (C++) | C++ | XQA Learn