Nested if...else Statement
Nested if...else Statements:
A nested if is an if statement that is the target of another if statement. Nested if statements means an if statement inside another if statement. Yes, C++ allows us to nest if statements within if statements. i.e, we can place an if statement inside another if statement.
The syntex of nested if...else statement is:
if (condition1)
{
Executes when condition1 is true
if (condition2)
{
Executes when condition2 is true
}
}
Flowchart of nested if else statemtent is show in figure:
Example of nested if else statement is:
#include <iostream>
using namespace std;
// C++ program to illustrate nested-if statement
int main()
{
int i;
cout << "Enter an integer numver";
cin >> i;
if (i == 10)
{
// First if statement
if (i < 15)
cout<<"i is smaller than 15";
// Nested - if statement
// Will only be executed if statement above
// it is true
if (i < 12)
cout<<"i is smaller than 12 too";
else
cout<<"i is greater than 15";
}
return 0;
}
Output:
i is smaller than 15
Comments
Post a Comment