C++ Nested Conditions
In this page:
if (outer_condition) {
if (inner_condition) {
// runs when both are true
}
}
What is a Nested Condition?
A nested condition is an if statement placed inside the body of another if statement, and the inner condition is only evaluated at all once the outer condition has already been satisfied.
Example: What is a Nested Condition?
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare outer and set it to true
bool outer = true;
// Check whether outer
if (outer) {
// Declare inner and set it to true
bool inner = true;
// Check whether inner
if (inner) {
// Print to the console with cout
std::cout << "Inner reached" << std::endl;
}
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Basic Nested if
In a basic nested if, the outer if statement's block contains a complete inner if statement, so reaching the innermost code requires every enclosing condition to be true.
Example: Basic Nested if
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare age and set it to 25
int age = 25;
// Declare hasTicket and set it to true
bool hasTicket = true;
// Check whether age >= 18
if (age >= 18) {
// Check whether hasTicket
if (hasTicket) {
// Print to the console with cout
std::cout << "Entry allowed" << std::endl;
}
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Nested if-else
An else branch can itself contain a complete if-else statement, letting nested conditions distinguish between more than two outcomes, such as separating three different age categories.
Example: Nested if-else
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare score and set it to 75
int score = 75;
// Check whether score >= 90
if (score >= 90) {
// Print to the console with cout
std::cout << "A" << std::endl;
} else {
// Check whether score >= 70
if (score >= 70) {
// Print to the console with cout
std::cout << "B" << std::endl;
} else {
std::cout << "C" << std::endl;
}
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Multiple Levels of Nesting
Conditions can be nested several levels deep to check increasingly specific combinations of criteria, though very deep nesting tends to make code significantly harder to read and maintain.
Example: Multiple Levels of Nesting
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare loggedIn and set it to true
bool loggedIn = true;
// Declare isAdmin and set it to true
bool isAdmin = true;
bool hasPermission = true;
// Check whether loggedIn
if (loggedIn) {
// Check whether isAdmin
if (isAdmin) {
if (hasPermission) {
// Print to the console with cout
std::cout << "Access granted" << std::endl;
}
}
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Nested Conditions vs Logical Operators
A nested if achieves the same result as combining conditions with the logical AND operator (&&) in a single if statement, and the logical-operator version is often considered more concise and readable for simple combined checks.
Example: Nested Conditions vs Logical Operators
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare age and set it to 25
int age = 25;
// Declare hasTicket and set it to true
bool hasTicket = true;
// Check whether age >= 18
if (age >= 18) {
// Check whether hasTicket
if (hasTicket) {
// Print to the console with cout
std::cout << "Nested: allowed" << std::endl;
}
}
if (age >= 18 && hasTicket) {
// Print to the console with cout
std::cout << "Logical AND: allowed" << std::endl;
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Nesting so many ifs that the code is hard to read, when
&&in one condition would do. - Misplacing braces so an
elseattaches to the wrong innerif. - Forgetting the closing brace of an inner block, causing compile errors far from the real problem.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: