Header Ads Widget

Responsive Advertisement

Nested if-else Statement with Syntax and Examples in C++

There are following types of conditional statements in C++ :
  • if statement
  • if - else statement
  • nested if-else statement
  • if -else if ladder
  • switch statement

3. Nested if-else statement - The "if" and "else" statements inside the body of “if” or "else" statements is "nested if-else". As the  conditions are tested in "if-else" the test goes through the "Nested if-else" .

Syntax :

if (condition)
   if(condition)
   {
       // Body of the loop.
       // These statements will only execute if the condition is true.
   }
   else
   {
       // Body of the loop.
       // These statements will only execute if the condition is false.
   }
}
else
   if(condition)
   {  
       // Body of the loop.
       // These statements will only execute if the condition is false.
   }
}


Flow Diagram of Nested if-else statement

Nested If in C Programming



Example of Nested if-else statement in C++


#include<iostream.h>

#include<conio.h>

int main()
{
   int  a=10,b=10;
   if(a<=b)  //if condition statement
    {
         if(a=b)  //nested if condition statement
         {
             cout<<"Value of a is equal to b."; //display output
         }
         else  //nested else condition statement
         {
             cout<<"Value of a is less than b";  //display output
         }
    } 
   else  //else condition statement
    {
       cout<<"Value of b is less than a"; //display output
    }
  getch();
}


OUTPUT:

Value of a is equal to b.

Post a Comment

0 Comments