C Lesson 5: Nested If-Else Statement C Program

Nested If-Else Statement C Program:

Nested If-Else Statement:

From the name itself it is understandable : If-Else statement within another If-Else Statement.


Here you can insert as many as If-Else statements within one another just like a nest. Hence called as nested If-Else statements.

Lets see simple comparison of "if", " if-else", "nested if " statements.

Condition statements Syntax  Explanation
If if (condition)
{ Statements; }
 If condition is true, then respective block of statements are executed.
If-Else if (condition)
{ Statements; }
{ Statements; }
The statements are executed when condition is true.
If condition is false, then else part is executed.
Nested If if (condition1){ Statements }
{ Statements }
else Statement 3;
If condition 1 is false, then condition 2 in else if is checked and statements are executed if it is true.
If condition 2 false, then else part is executed.

Now lets see how it works through simple example.

Example Nested If-Else Statement C Program:

Sample Output :

Program Explanation:

  1. Get two inputs num_1, num_2 (variables) from user through
  2. Check whether the given two numbers are equal or not using If statement. if the condition is true, both the numbers are equal.
  3.  If the condition is false, again  use a nested Else-If statement . Here check if the first number is greater than second number. if the condition is true print as "num_1>num_2"
  4. If the above condition is false, we go for else statement. Print the second number is greater than first number.
  5. Using nested If statement, we check if the two numbers are equal otherwise which number is greater.

Thanks for reading...

Read More:

Embedded C Programming for Electrical Engineers
8051 Instruction Set List for Reference 
Aurdino Programming Commands Reference Guide

Try various types of nested If Programs using many conditions.
You can easily understand the working of nested if-else statements by doing many such programs....Please leave your comments below....

You may also like...

1 Response

  1. Srilu says:

    Nice explanation

Leave a Reply

Your email address will not be published. Required fields are marked *