C++ nested if Control Statement
Nested if control statement refers to if condition within if condition. C++ compiler checks nested if control statement when its parent if condition returns true, then the program is executed according to nested if condition.
Syntax
1 2 3 4 5 6 7 8 9 10 11 |
if(check_condation){ statement 1; ...; if(check_condation){ statement I; ... } ...; } |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <iostream> using namespace std; int main () { int a=10; int b=20; if( a < b ) { cout << "a is less than b:" << endl; if(b>a){ cout<<"b is greater than a:"<<endl; } } cout<<"value of a is: "<<a <<" value of b is: "<<b; return 0; } |
Output
1 2 3 4 5 |
a is less than b: b is greater than a: value of a is: 10 value of b is: 20 |