Nested if…else and if…else ladder in C

Spread the love




Nested if-else and if-else ladder  

In our previous post, we discussed the if and if…else statements. Today we will discuss Nested if…else and if…else ladder.

Sometimes we need to use if statement inside the existing if statement.  So  if you have if statement inside the if statement then that is called “Nested if statement”

Syntax of Nested if…else statement :

In the above code if statement with condition2 is nested if statement. 

Example to demonstrate nested if…else :

Let’s say you want to find the largest number among the 3 numbers. Numbers are A,B and C. To find the largest number first we need to find the large number between the A and B. If the A is bigger we will go to the if condition and checks if the A is also bigger than the C. If so then A is the largest among the three numbers.

if…else ladder :

if we have a chain of if and else blocks then we call it as if…else ladder. only one block among all if..else blocks executed and remaining all blocks will be ignored.

Syntax of if..else ladder

Only one of the all if and else..if and else blocks will be executed

If any of the conditions are true then that particular block of code will be executed if none of the conditions are true then final else block will be executed. Whenever we need to make a decision based on multiple conditions we use the if..else ladder.

If you have too many conditions if..else ladder will becomes difficult to maintain and code readability also degrades. So when you have too many conditions use the switch case instead of if..else ladder

Venkatesh Macha

Hi Guys, I am Venkatesh Macha. I am a programmer and an Open Source enthusiast, Presently working as a Software Engineer. Apart from that, I love to explore new technologies and things.

You may also like...

Leave a Reply