Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Nested IF ELSE Statement in C

Last Updated on March 2, 2023 by Prepbytes

In this article, we are going to explore nested if-else statement in c with syntax, flowcharts, and example. We will also look at nested if statement in c with syntax and example. At the end of the article, we will discuss some advantages and disadvantages of using nested if-else statements.

Introduction to If-Else Statement

In programming, if-else statements are used to make alternative conclusions based on particular criteria being met. Because of this, these statements are referred to be decision-making statements. They aid in making judgments based on conditions and hence contribute to determining the flow of the code.

If the condition contained within the if bracket is true, the statements contained within the if bracket is executed. If the condition is found to be false, the if block is bypassed and the statements inside the otherwise block are executed.

For example, given a number, we must determine if it is prime or not. That means we must decide whether to print prime or not prime. This is why we use the if-else statement in c.

if n is prime 
    print prime
else
    print not a prime

Nested If Else Statement

We’ve already seen how useful if and else statements are, but what if we need to check for more conditions even if one is met?

For example, if we need to determine whether a number is even or odd and if it is even, whether it is divisible by 8 or not, and if it is odd, whether it is divisible by 7 or not. In this scenario, a single if-else statement would be insufficient.

To begin, we will use one if-else statement to determine whether the integer is even or odd. Then, in the if block, if the number was even, we’d have to include another if and else statement to see if it’s divisible by 8 or not.

Similarly, in the else block, we would need to include another if and else statement to determine whether the integer is divisible by 7.

Nesting is the practice of including multiple if-else statements within an if and else statement. The second set of if and else statements is considered to be nested within the first.

This is why the C language supports the nesting of if and else expressions. These are known as nested if-else statements, and they allow for more precise decision-making when additional requirements must be tested within the first conditions, like in the prior example.

Syntax of Nested If-Else Statement

Let us see the syntax of nested if-else statements in c:

if (condition1){
   if (condition2)
      line1;
   else
      line2;
}
else{
   if (condition3)
      line3;
   else
      line4;
}

Flow Chart of Nested If-Else

Elaboration
In the preceding example, the first IF statement checks to see if n is even. If this n is even, condition 1 evaluates to true, and we proceed inside the if block. Here is our nested if-else statement, which checks to see if n is divisible by 8. If condition 2 returns true, we proceed to execute line 1. We print here that the integer is even and divisible by 8. If condition 2 is false, we enter the else statement and print that the number is even but not divisible by 8.

Similarly, if condition 1 evaluates to false, we skip to the ELSE block and check the condition of the nested if statement. We enter the nested if condition 3 evaluates to true. We print here that the number is odd and divisible by 7. If condition 3 is false, we enter the nested else statement and print that the number is odd but not divisible by 7.

The important thing to remember here is that when we enter the if block, the otherwise block is ignored, and when we enter the else block, the if block is ignored, and this is exactly what happens.

Above, we go through an example of a nested if-else statement in c. Let’s see the code implementation of the above example in c.

Code Implementation of Nested If-Else:

#include <stdio.h>

int main() {

  // implementation starts from here
  
  // storage variable declared
  int n;
  
  //taking input
  scanf("%d", &n);

  //if else condition to check whether the number is even or odd
  if (n % 2 == 0) {

    //the number is even
    printf("Even ");

    //nested if else condition to check if n is divisible by 8 or not
    if (n % 8 == 0) {
      //the number is divisible by 8
      printf("and divisible by 8");
    } else {
      //the number is not divisible by 8
      printf("and not divisible by 8");
    }
  } else {
    //the number is odd
    printf("Odd ");

    //nested if else condition to check if n is divisible by 7 or not
    if (n % 7 == 0) {
      //the number is divisible by 7
      printf("and divisible by 7");
    } else {
      //the number is not divisible by 7
      printf("and not divisible by 7");
    }

  }

  return 0;
}

Input:

21

Output:

Odd and divisible by 7

Nested If Statement in C

In C, a nested if statement is a construct where an if statement is placed inside another if statement. The inner if statement is only evaluated if the condition of the outer if statement is true.

It is also possible to have multiple nested if statements, where each inner if statement is executed only if the outer if statement’s condition is true.

Nested-if statements can be helpful when you have multiple conditions to check, and the conditions are dependent on each other. It can simplify complex conditions and improve the readability and efficiency of the code. However, it’s important to use them judiciously, as overuse of nested if statements can make code more complex, harder to read, harder to debug and increase execution time and memory usage.

Syntax of Nested If Statement

if (condition1) {
    // code to be executed if condition1 is true
    if (condition2) {
        // code to be executed if condition2 is true
    }
}

the inner if statement will only be executed if the outer if statement’s condition (condition1) is true. If condition 1 is false, the program will skip over the inner if statement and the control flow will continue with the next statement after the nested if statement.

Advantages of using Nested If-Else Statement

There are several advantages to using nested if-else statement in C:

  • Improved readability: Nesting if-else statement can make code more readable by making it clear which conditions must be met in order for certain actions to be taken.
  • Handling multiple conditions: Nesting if-else statement allows you to handle multiple conditions at once, which can be useful when the conditions are dependent on each other.
  • Reusability: Nesting if-else statement can help to make code more reusable by allowing you to use the same conditions in multiple places within your code.
  • Improved efficiency: By using nested if-else statement, you can improve the efficiency of your code by only executing certain statements if the conditions are met.
  • Simplify complex conditions: By breaking down complex conditions into multiple nested if-else statement, it makes it easier to understand and debug the code.

It’s worth noting that, while nested if-else statement can be useful in certain situations, they can also make code more difficult to read and understand if overused or used in a confusing way. So it’s important to use them judiciously.

Disadvantages of using Nested If-Else Statement

There are several disadvantages to using nested if-else statement in c:

  • Increased complexity: Nesting if-else statement can make code more complex, which can make it more difficult to understand and debug.
  • Decreased readability: Nesting if-else statement can make code less readable, especially if there are multiple levels of nesting.
  • Increased likelihood of bugs: With increased complexity and decreased readability, the likelihood of bugs in the code also increases.
  • Hard to maintain: When the requirements of the program change, it’s hard to maintain the code and make necessary updates, especially when there are multiple levels of nesting.
  • Decreased performance: Nesting an if-else statement can decrease the performance of your code because the program has to evaluate multiple conditions.

It’s worth noting that, while nested if-else statement can be useful in certain situations, they can also make code more difficult to read and understand if overused or used in a confusing way. So it’s important to use them judiciously and try to avoid excessive nesting.

Summary

  • Nested if-else statement in C is a construct where an if-else statement is placed inside another if-else statement.
  • The inner if-else statement is executed only if the outer if-else statement’s condition is true.
  • Nested if-else statement in C can be useful when handling multiple conditions that are dependent on each other and make code more readable.
  • Overuse of nested if-else statements can make code more complex, harder to read, harder to debug and increase execution time and memory usage.
  • Nested if statements in C are a construct where an if statement is placed inside another if statement.
  • The inner if statement is only evaluated if the condition of the outer if statement is true.
  • Writing if statement within another if is called as nested if statement.

Leave a Reply

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