Nested If Else Statement In C

A nested if-else statement refers to an if statement contained within another if statement. The typical structure of a nested if-else statement in C appears as:

Example

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

As observed, the outer if statement presents two potential routes: one for a true condition and another for a false condition. When the condition evaluates to true, the program will proceed with executing the code enclosed within the corresponding block of the outer if statement. Conversely, if the condition is false, the program will bypass that block and advance to the else block. Nested within the outer if block is an additional if statement, which similarly offers two potential routes based on the truth or falsity of its condition.

Example:

To demonstrate the functionality of a nested if-else statement, let's explore a scenario. Let's say we aim to create a program that receives a numeric input and verifies if it is positive, negative, or zero.

Example

#include <stdio.h>

int main() {
   int num;

printf("Enter a number: ");
scanf("%d", &num);

   if (num > 0) {
printf("%d is positive.\n", num);
   } else {
      if (num < 0) {
printf("%d is negative.\n", num);
      } else {
printf("%d is zero.\n", num);
      }
   }

   return 0;
}

Output:

Let's execute the aforementioned program using various example inputs and observe the resulting output.

Example

Enter a number: 10
10 is positive.

Enter a number: -5
-5 is negative.

Enter a number: 0
0 is zero.

Explanation:

In this software, we initially request the user to input a numeric value, which is then captured through the scanf function. Following this, a nested if-else construct is employed to determine if the input number is positive, negative, or zero. The primary if statement assesses if the number is above zero. If affirmative, a message indicating the positivity of the number is displayed. Should the condition be false, the program proceeds to the else segment. Enclosed within the else segment is another if statement that verifies if the number is negative. In case of a negative number, a corresponding message is printed. Otherwise, the program advances to the final else block which notifies that the number is zero. Evidently, the software accurately discerns whether the entered number is positive, negative, or zero, and exhibits the relevant message.

Conclusion:

To summarize, nested if-else constructs play a crucial role in programming by enabling programs to make intricate decisions depending on various conditions. In C programming, nested if-else statements are achieved by placing an if statement within another if statement. The structure of nested if-else statements is simple, and the illustration provided in this article illustrates the utilization of nested if-else statements to determine if a number is positive, negative, or zero. Through the incorporation of nested if-else statements, programmers can develop more intricate programs capable of making sophisticated decisions based on numerous conditions.

It's crucial to understand that nested if-else statements can become complex if there are numerous conditions to evaluate. In such scenarios, opting for alternative control flow mechanisms like switch statements or loops might be more suitable. Furthermore, maintaining proper indentation and formatting of nested if-else statements is essential to enhance the readability and sustainability of the code.

Moreover, it is crucial to guarantee that the criteria specified in nested if-else constructs are clearly defined and encompass all potential scenarios. Neglecting this aspect may lead to unpredictable outcomes and flaws within your software.

Input Required

This code uses input(). Please provide values below: