Nested if else statement

If else statement is the conditional statement.

Conditional statement :- it means we can change flow of executing program based on condition in program.

In previous tutorial we seen the if else statement.

If else statement :  if else statement is the simple and commonly used conditional statement in c++. If statement contains condition , if this conditional is true then the statements followed by if statement is executed otherwise statement in else block get executed.

Nested if else statement :- using if else statement multiple time one in another is known as nested if else. We write if else statement and derive another if else within it.

We can use multiple if else for one inside another.

Syntax for nested if else statement in c++


123456789101112131415161718192021222324252627282930313233343536373839if(condition){Statement 1;Statement 2;.Statement n;}else if(condition){Statement 1;Statement 2;.Statement n;}else {Statement 1;Statement 2;.Statement n;}

In above syntax ,

1. We write if keyword followed by condition in bracket (). and after that we write statement in block enclosed in curly braces { }. You can write any number of statement in if block enclosed in { } . if you want to execute only only one statement in if block then you not need to write curly braces.In this case statement (one )followed by if block is executed similarly for else block.

2. if condition is true then statement in of block get executed else another condition is checked if condition is true then statement in else if executed otherwise the statement in else block get executed.


1234567891011if(x>10)cout<<“x value greater than 1”;else if(x> 5)cout<<“x value greater than 5”;elsecout<<“x value less ;

In above example, if statement checks value of x if it is greater than 10 then statement x value greater than 10 is executed otherwise another condition x>5 is checked if it is true then x value greater than 5 executed otherwise statement in else block x value less is executed.

Program to demonstrate nested if else statement

1234567891011121314151617181920212223242526272829303132333435363738394041#include<iostream.h>#include<conio.h>void main (){int x;cout<<“Nested if _ else statement in cpp”<<endl;cout<<“Enter x value”<<endl;cin>>x;if(x>10) //if statement to check condition{cout<<“x value is greater than 10” //executed if condition true}else if(x>5){cout<<“x value is greater than 5”;. //executed if condition is false}else{cout<<“x value is less”;}getch();}

Output :-

Nested if _ else statement in cpp

Enter x value


x value greater than 5

Program description

  1. In above program we demonstrate nested if else statement.
  2. First we taken input from user in variable x
  3. Then first if condition we check condition if(x>10) if user enters value greater than 10 then if condition if (x > 10) get true and statement in if (i.e x value greater than 10) is executed and statement in if else and else block get skipped.
  4. And if user enters value less than 10 but grater than 5 then else if condition else if(x> 5) get true and statement in else if block executed and statement in if and else block get skipped.
  5. And if user enter value less than 5 then both condition get false and statement in else block get executed.