Nested if & if/else Statements in C++

Nested if statements:

You can also include, or nest, if statements within another if statement.
For example:

int mark = 100;

if (mark >= 50) {
  cout << "You passed." << endl;
  if (mark == 100) {
    cout <<"Perfect!" << endl;
  }
}
else {
  cout << "You failed." << endl;
}

Output:

You passed.
Perfect!

Nested if else Statements:

C++ provides the option of nesting an unlimited number of if/else statements.
For example:

int age=18;
if(age>14)
{
  if(age>=18)
 {
    cout<<"Adult";
 }
  else
 {
    cout<<"Teenager";
 }
}
else
{
  if (age > 0)
 {
    cout<<"Child";
 }
  else
 {
    cout << "Something's wrong";
 }
}

Output:

Adult

Share Tweet Send
0 Comments
Loading...
You've successfully subscribed to Developer Insider
Great! Next, complete checkout for full access to Developer Insider
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.