In this tutorial you will learn about the C++ Nested if else Statement and its application with practical example.
C++ Nested if else Statement
In C++, when there is an if statement inside another if statement then it is known as nested if else. Nested if else can also be simplified using C++ Switch Case Statement.
Syntax:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
if(condition1) { if(condition2){ // statements } else { // statements } } else { if(condition3){ // statements } else { // statements } } |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> using namespace std; int main() { int a = 25; cout<<"W3Adda - C++ Nested If Statement"; if(a < 100){ if(a < 50){ cout<<"\na is less than 50"; } if(a >= 50){ cout<<"\na is greater than 50"; } } return 0; } |
When we run the above C++ program, will see following output –
Output:-