Nested if else and if else ladder in C

Nested-if-else-and-if-else-ladder-in-c-programming-language

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

if..else ladder example program:

We can calculate the student grades using the if..else ladder. Because we need to compare the different marks to calculate the grade.

Here is the code for the student grades program

Output:

We are using the gcc compiler to run the program. you can use any C-lang compiler.

We have explained the student’s grade program in detail in the following article, Please check it for details.

Conclusion:

We have discussed the Nested if else and if..else ladder.

Related Articles:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

8 Responses

  1. […] Nested if else and if..else ladder in C – SillyCodes […]

  2. […] Nested if else and if..else ladder in C-Language […]

  3. […] have discussed the Nested-if-else and if else ladder in an earlier article. In today’s article, we will look at the Switch Statement in C […]

  4. […] Nested if else and if..else ladder in C-Language […]

  5. […] Nested if else and if..else ladder in C-Language […]

  6. […] Nested if else and if..else ladder in C-Language […]

  7. […] Nested if else and if..else ladder in C-Language […]

  8. […] Nested if else and if..else ladder in C-Language […]

Leave a Reply