In C++ the braces of and if or an else clause can contain another if statement. There are known as nested if statements.
In simple terms a nested if is when you write an IF inside another IF braces.
This is the basic structure of a nested if.
if( boolean_expression 1) { // Executes when the boolean expression 1 is true if(boolean_expression 2) { // Executes when the boolean expression 2 is true } }
And of course we can nest else if in the similar way as you nest the if statement.
This is an example of a program with nested if-else.
#include <iostream> using namespace std; int main () { int marks = 55; if( marks >= 80) { cout << "U are 1st class !!"; } else { if( marks >= 60) { cout << "U are 2nd class !!"; } else { if( marks >= 40) { cout << "U are 3rd class !!"; } else { cout << "U are fail !!"; } } } return 0; }
And this will be the output:
U are 3rd class !!
-The Admin.
Nesting of conditional IF statements. by esaupreciado is licensed under a Creative Commons Attribution 4.0 International License.