Javatpoint Logo
Javatpoint Logo

Nested if else statement in C

One of the fundamental constructs in programming is conditional statements. They allow a program to take different paths based on the values of certain conditions. In C, conditional statements are implemented using if-else statements. In more complex scenarios, nested if-else statements can be used to make more sophisticated decisions. This blog post will provide an in-depth explanation of nested if-else statements in C, including syntax, example, and output.

Syntax:

A nested if-else statement is an if statement inside another if statement. The general syntax of nested if-else statement in C is as follows:

As you can see, the outer if statement has two possible paths: one for when the condition is true, and another for when the condition is false. If the condition is true, the program will execute the code inside the block associated with the outer if statement. However, if the condition is false, the program will skip over that block and move to the else block. Within the outer if block, there is another if statement, which can also have two possible paths depending on whether the condition is true or false.

Example:

To illustrate how a nested if-else statement works, consider the following example. Suppose we want to write a program that takes in a number and checks whether it is positive, negative, or zero.

Output:

Let's run the above program with some sample inputs and see the output.

Enter a number: 10
10 is positive.

Enter a number: -5
-5 is negative.

Enter a number: 0
0 is zero.

Explanation:

In this program, we first prompt the user to enter a number, which is then read in using scanf(). After that, we use a nested if-else statement to check whether the number is positive, negative, or zero. The outer if statement checks whether the number is greater than zero. If it is, the program prints a message saying that the number is positive. If it is not, the program moves to the else block. Within the else block, there is another if statement that checks whether the number is less than zero. If it is, the program prints a message saying that the number is negative. If it is not, the program moves to the else block, which is the final block. This block prints a message saying that the number is zero. As you can see, the program correctly identifies whether the input number is positive, negative, or zero, and prints the appropriate message.

Conclusion:

In conclusion, nested if-else statements are an important construct in programming that allows a program to make more sophisticated decisions based on multiple conditions. In C, nested if-else statements are implemented using an if statement inside another if statement. The syntax of nested if-else statements is straightforward, and the example we discussed in this blog post demonstrates how to use nested if-else statements to check whether a number is positive, negative, or zero. By using nested if-else statements, we can write programs that are more complex and able to make more sophisticated decisions based on multiple conditions.

It is important to note that nested if-else statements can quickly become unwieldy if there are too many conditions to check. In such cases, it may be more appropriate to use other control flow constructs, such as switch statements or loops. Additionally, it is important to ensure that nested if-else statements are properly indented and formatted to improve code readability and maintainability.

Additionally, it's important to ensure that the conditions used in nested if-else statements are well-defined and cover all possible cases. Failure to do so can result in unexpected behavior and errors in your program.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA