Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Recap of If Else Statement
2.1.
Syntax:
2.2.
C
3.
Nested If else statement in C
4.
Syntax of Nested If else statement in C
5.
Explanation of How Nested If-else Works
6.
Flowchart of Nested If Else Statement in C
7.
Working of Nested If Statement in C
8.
Examples for Nested If Else Statement in C
8.1.
Example 1
8.1.1.
Implementation
8.2.
C
8.3.
Example 2
8.3.1.
Implementation
8.4.
C
8.5.
Example 3
8.5.1.
Implementation
8.6.
C
8.7.
Example 4
8.7.1.
Implementation
8.8.
C
9.
Types of if-else statements in C
10.
Frequently Asked Questions
10.1.
What is the difference between nested if and else if?
10.2.
What is cascaded if-else syntax?
10.3.
What is the syntax for if-else loop?
10.4.
What is the structure of if-else in C?
10.5.
How many times can I use nested if-else statements in C?
10.6.
What is the statement that can avoid multiple nested if conditions?
10.7.
Why is it advisable to avoid nested conditional statements?
10.8.
What is the if-else if statement in C?
10.9.
Is Nested If a Looping Statement?
11.
Conclusion
Last Updated: Sep 30, 2024
Easy

Nested If Else Statement in C

Author Nidhi Kumari
1 upvote

Introduction

Decision-making is one of the vital features of any programming language. The nested if else statement in C is used for decision-making. If-else statements choose which code block will be executed when a particular condition is satisfied. 

One can use a simple if-else conditional statement when it only chooses one of the two possibilities. For choosing one option from several options, nested if-else statements are used. 

Nested If else statement in C

 

Also, see conditional statements and conditional statements in C.

Recap of If Else Statement

Let's start our discussion by understanding the if else statement in C. The if else statement is a part of conditional statements in C. If given conditions are true, the if/else statement enables a code block to be executed. Another line of code can be executed if the condition is false.

Syntax:

The syntax of the if else statement in C.

if( some conditional statement){
// Do something
}

else{
// Do something
}

 

For example, we have a number. We need to check whether the number is positive or not. Using an if else statement to code the above condition:

if( number > 0){
return Positive number;
}
else if( number ==0){
return Zero;
}


else{
return Negative number;
}

 

Here is the complete code:

  • C

C

#include <stdio.h>
int main()
{
   int number;

  // Take user input
   scanf("%d", &number);
  
   if (number > 0)
       printf("%d is a positive number \n", number);
      
   else if( number ==0)
        printf("Given number is zero");
   else
       printf("%d is a negative number \n", number);
      
return 0;
}
You can also try this code with Online C Compiler
Run Code

Input 1:

3

 

Output 1:

3 is a positive number

 

Input 2: 

-3

 

Output 2:

-3 is a negative number 

 

Input 3:

0

 

Output 3:

The given number is zero

Nested If else statement in C

We have discussed a simple conditional statement, i.e., if-else statement. What if we need to check multiple conditions? For example, we need to check whether the number is positive and greater than 10. We need nested conditional statements(nested if else statements) to deal with such a problem in C. In nested if-else statements, an if-else statement may appear inside the body of another if-else statement. 

An if statement that is nested inside another if statement is known as an if-else statement. This indicates that only if the outer if statement is true will the inner if statement be carried out. If the result of the outer if statement is true, the inner if statement is then carried out. The inner if statement is skipped if the outer if statement returns false.

Syntax of Nested If else statement in C

The syntax of a nested if else statement in C is shown below:

if (Conditional statement 1)
{
     if(Conditional statement 2)


     {
          Statement 1;
      }
     else
     {
          Statement 2;
     }
}
else
{
     if(Conditional statement 3)
     {
          Statement 3;
     }
     else
     {
          Statement 4;
     }
}

Explanation of How Nested If-else Works

  • Nested if-else statements are a series of conditional statements contained within each other.
  • They allow for more complex decision-making by evaluating multiple conditions in a hierarchical manner.
  • When the outer condition is true, the program evaluates the inner condition(s) sequentially.
  • If the outer condition is false, the program skips the inner condition(s) and moves to the next statement following the nested if-else block.
  • Each if-else statement within the nested structure operates independently, allowing for different actions based on various combinations of conditions.
  • Nested if-else statements can be nested to multiple levels, but excessive nesting can make code harder to read and understand.
  • Proper indentation is crucial for maintaining clarity and readability in nested if-else structures.
  • Nested if-else statements provide a way to handle more complex decision-making scenarios in programming.

Flowchart of Nested If Else Statement in C

To better understand the concept of the nested if else statement in c, let's discuss the flowchart of the nested if else statement in C.

flowchart of nested If else statement in c

Working of Nested If Statement in C

Case 1: Check the conditional statement 1. If the first condition is true, go for the second condition.
 

  • Case 1.1: If Condition 2 evaluates to TRUE, the program will execute Statement 1.
     
  • Case 1.2: Otherwise, the program will execute Statement 2.
     

Case 2: If the first condition is false, go for the third one.
 

  • Case 2.1:  If Condition 3 evaluates TRUE, the program will execute Statement 3.
     
  • Case 2.2: Otherwise, the program will execute Statement 4.


Also see, Floyd's Triangle in C

Examples for Nested If Else Statement in C

Here are some examples of the nested if else statement in C.

Example 1

Consider an example where a person is eligible to participate in CODE STUDIO Coding Contest only if they are above 18 and below 25.

Implementation

  • C

C

#include <stdio.h>


int main() {
 int age;
 // Take present age as input
 scanf("%d", & age);


 if (age > 18) {
   if (age < 25) {
     printf("Ready to start the contest, NINJA! \n");
   } else {
     printf("Not eligible\n");
   }
 }


else {
   printf("You are not eligible.\n");
 }
 return 0;
}
You can also try this code with Online C Compiler
Run Code

Input:

21

 

Output:

Ready to start the contest, NINJA! 

 

Example 2

If a person scores more than 50 and less than 100 points in the CODE STUDIO Coding Contest, they will get a Coding ninjas T-shirt. If he/she scores less than 50 points, they get a Diary. For points more than 100, they will get a diary and a pen.

Implementation

  • C

C

#include <stdio.h>


int main() {
int p;
// Taking point as user input
scanf("%d", & p);

if (p > 50) {
// Outer if statement
if (p < 100) {
printf("Hooray! Your prize is a Coding Ninjas T-shirt.\n");
}

else {
printf("You prize is a Coding Ninjas T-shirt and Diary");
}
}

else {
printf("You prize is a Coding Ninjas Diary");
}

return 0;
}
You can also try this code with Online C Compiler
Run Code

Input:

90

 

Output:

Hooray! Your prize is a Coding Ninjas T-shirt.


Also see, Tribonacci Series and  Short int in C Programming

Example 3

Ninja is playing with numbers. He wanted to write a program that could find the maximum number. Every time the ninja enters three values. Can you help him?

We can solve this problem by using the nested if else statement in C.

Implementation

  • C

C

#include<stdio.h>

int main() {
int n1, n2, n3;
scanf("%d%d%d", & n1, & n2, & n3);

if (n1 > n2) {
if (n1 > n3) {
printf("The largest number is %d", n1);
}

else {
printf(" The largest number is %d", n3);
}
}

else {
if (n2 > n3) {
printf("The largest number is %d", n2);
}

else {
printf("The largest number is %d", n3);
}
}
return 0;
}
You can also try this code with Online C Compiler
Run Code

Input:

800 480 760

 

Output:

The largest number is 800

Example 4

Discount Based on Membership and Purchase Amount. Let us implement this using nested if else statement in C.

Implementation

  • C

C

#include <stdio.h>

int main() {
int is_member, purchase_amount;
float discount = 0.0f;

printf("Enter 1 if member, 0 otherwise: ");
scanf("%d", &is_member);
printf("Enter purchase amount: ");
scanf("%d", &purchase_amount);

// Apply nested if-else to determine discount
if (is_member) {
if (purchase_amount >= 100) {
// 10% discount for members with purchase >= rs100
discount = 0.1f * purchase_amount;
} else {
// rs5 discount for members with purchase < rs100
discount = 5.0f;
}
} else {
if (purchase_amount >= 200) {
// 5% discount for non-members with purchase >= rs200
discount = 0.05f * purchase_amount;
}
}

// Display the final discount amount
printf("Discount amount: rs%.2f\n", discount);

return 0;
}
You can also try this code with Online C Compiler
Run Code

Output:

Enter 1 if member, 0 otherwise: 1
Enter purchase amount: 300
Discount amount: rs30.00

 

Check out this problem - First Missing Positive , C Static Function

Must Read Decision Making in C

Types of if-else statements in C

In C, there are 4 different kinds of if-else statements:

Simple if: The simplest kind of if-else expression is one like this. It has one block of code and one condition.

If-else: There are two conditions in this kind of if-else expression. First, the first condition is examined. The first block of code is run if the statement is accurate. The second condition is examined if the first one is untrue. The second block of code is run if the second condition is true.

If-else-if: An if-else statement with three or more criteria is called an if-else-if statement. First, the first condition is examined. The first block of code is run if the statement is accurate. If not, the second stipulation is examined. The second block of code is run if the second condition is true. This keeps on until either a condition is verified as true or all conditions have been tested and verified as false.

Nested If-else: An if-else statement that is nested inside another if-else statement falls under this category. This indicates that the inside if-else statement will only be executed if the outer if-else expression is true.

Frequently Asked Questions

What is the difference between nested if and else if?

The primary distinction between nested if and else if is that the former consists of an if statement inside of another if statement, whilst the latter is made up of a number of if statements joined together by else statements.

What is cascaded if-else syntax?

If statements joined by else if statements make up a cascaded if-else syntax. It is applied to examine several circumstances consecutively. 

What is the syntax for if-else loop?

The syntax for an if-else loop in most programming languages is as follows:

if (condition) {
 // code block to be executed if condition is true
} else {
 // code block to be executed if condition is false
}

What is the structure of if-else in C?

The structure of if-else in C is:

if (condition) {
 // code block to be executed if condition is true
} else {
 // code block to be executed if condition is false
}

How many times can I use nested if-else statements in C?

Theoretically, there is no such limit for the depth of the nested if else statement in C, but practically it depends on the design of the compiler's parser. Some language standards specify a minimum supported number or even a maximum depth. It can also have performance implications if the conditions being tested are computationally expensive, as each level of nesting requires an additional evaluation of the condition.

What is the statement that can avoid multiple nested if conditions?

The statement that can avoid multiple nested if conditions are the switch statement. The switch statement allows you to evaluate a variable or expression against a set of values or conditions and execute different code blocks based on the matching case. It provides a more efficient and readable alternative to using multiple nested if statements, especially when dealing with many possible conditions.

Why is it advisable to avoid nested conditional statements?

It is advisable to avoid nested conditional statements because they reduce code readability, make the code more complex, and negatively impact the program's performance. Additionally, nested conditional statements can have performance implications, as each nested condition must be evaluated for every iteration of the loop or execution of the function, which can impact the speed and efficiency of the code.

What is the if-else if statement in C?

The if-else if statement is used for multiple conditions. It checks conditions sequentially, executing the first true block and skipping the rest.

Is Nested If a Looping Statement?

No, a nested if statement is not a looping statement. It is a conditional statement used to make decisions based on multiple conditions.

Conclusion

This article covered the nested if else statement in C and the advantages and disadvantages of the nested if else statement in C. The nested if-else statements let you check multiple conditions simultaneously and provide the correct result.

We hope this blog has helped you. We recommend you visit our articles on different topics of C language, such as

If you liked our article, do upvote our article and help other ninjas grow. You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences and interview bundles, follow guided paths for placement preparations, and much more!!

Happy Reading!!

Live masterclass