top

Search

C Tutorial

.

UpGrad

C Tutorial

Nested if else statement in C

If else statements are useful for decision-making applications. They allow you to specify which code block to be executed when a specific condition is fulfilled. Nested if else statements are types of if-else statements incorporated inside other if-else statements to achieve better decision-making.

Introduction to Nested if else Statement in C

When the matter comes to making a decision, we usually ask questions. Depending on the conditions, we analyse which path to accept. The order matters because the conditions are assessed sequentially. Implementing nested if-else statement in C++ and C lets you develop programs that make informed decisions depending on different conditions. These statements enhance your code’s flexibility and make it more versatile.

A nested if else statement in C allows testing multiple conditions and executing different code blocks based on the results. It is important to pay attention to the logical structure and ensure the proper formulation of each condition to avoid any unexpected behaviour.

if statement in C

In C, the if statement is the simplest decision-making statement. It helps you to determine whether a specific statement or block of statements needs to be executed or not. If the specified condition is true, then a block of statements executes, else it doesn’t.

Syntax of if statement in C

if(condition)
{
// Statement or block of statements to be executed if
// condition is true
}

Working of If Statement in C

The condition in a nested if-else statement is evaluated as either true or false. The if statement accepts boolean values, and if the condition is true, the block of statements following it is executed; otherwise, it is not executed.

Flowchart of if statement in C 

Input/Output

Input:

Typically, the input for this statement is a condition that can be assessed as either true or false. The condition’s evaluation is based on constants, variables, or expressions.

Output:

The output of this statement is the implementation of a block of code related to the true condition.

Example of if statement in C

#include <stdio.h>
int main()
{
int x = 15;
if (x > 20) 
{
printf("15 is greater than 20");
}
printf("The statement executed is outside the if block");
}

Output:

The statement executed is outside the if block

The above code shows that the given number is not greater than 20, so the program executes the statement mentioned outside the if statement.

if else in C

The if statement simply informs you that if a condition is true, then it will execute a block of statements. However, if the condition becomes false, it wouldn’t execute.

If you want to execute some other block of statements if the condition is false, then you can use the ‘if else’ statement in C.

Syntax of if else in C

if (condition)
{
    // Block of statements executed if the condition is true
}
else
{
    // Block of statements executed if the condition is false
}

Working of If else in C

The block of code mentioned in the else statement is executed whenever the condition declared in the if statement returns false.

Input/Output

Input:

Usually, the input for an if-else statement is a condition that can be assessed to be either true or false. Note that this condition depends on constants, variables, or expressions.

Output:

The output of this type of statement in C is the execution of code mentioned in either the “if” block or “else” block. The execution happens based on the condition evaluation.

Example of if else in C

#include <stdio.h>
int main()
{
int x = 25;
if (x < 20) 
{
printf("The number is smaller than 20");
}
else 
{
printf("The number is greater than 20");
}
return 0;
}

Output:

The number is greater than 20

In the above program, the given number is not smaller than 20, so the code mentioned inside the else block will be executed. Hence, the output prints, “The number is greater than 20”.

if-else-if ladder in C

The if-else-if ladder statements in C are used when the user has to determine among multiple options.

Syntax of if-else-if ladder in C

if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;

Working of if-else-if ladder in C

The if statements are implemented from the top down. When one of the conditions regulating the if is true, the statement linked with the corresponding if block is executed. The remaining C else-if ladder is bypassed.

In case none of the conditions evaluates to be true, then the final else statement would be executed.

Input/Output

Input:

The input for this type of statement is the expressions or values used in the conditions. Usually, such conditions are boolean expressions that assess either true or false.

Output:

The output is the execution of the code block related to the condition that returns true. If no conditions are true, the code block mentioned within the else statement is executed.

Examples

#include <stdio.h>
int main()
{
int marks;
printf("Please enter your marks: ");
scanf("%d", &marks);
if (marks >= 90)
{
printf("Grade is: A\n");
}
else if (marks >= 80)
{
printf("Grade is: B\n");
}
else if (marks >= 70)
{
printf("Grade is: C\n");
}
else if (marks >= 60)
{
printf("Grade is: D\n");
}
else
{
printf("Grade is: F\n");
}
}

Output:

Please enter your marks: 95
Grade is: A

In the above program, the user is allowed to enter the score. The program uses an if-else-if ladder to decide the grade depending on the score. All conditions evaluate in sequence, and the relevant code block is executed if the condition returns true.

The ladder begins with grade A and reaches down to grade F. If no conditions match, the statements inside the else block are executed.

Nested if-else in C

Incorporating multiple if-else statements within an if and else statement is known as nesting. Nested if-else in C involves using an if-else statement inside the if statement block.

Syntax of Nested If Else Statement in C

Here’s the syntax of the nested if else statement in C.

if (condition-1)
{
// Executes when condition-1 returns true
if (condition-2)
{
// Executes when condition-2 returns true
}
else
{
// Executes when condition-2 returns false
}

Working of Nested If Statement in C

The working shows placing an if statement within another if statement. The second “if-else” statements are nested within the first “if-else” statement. The nested if statement in C has a clearer decision-making ability than other conditional statements in C. It is useful when you want to check certain extra conditions within the initial conditions.

Input/Output

Input:

The input comprises the expressions or values used in the conditions.

Output:

The output is the execution of code corresponding to the condition that returns true.

Example of Nested If Else Statement in C

Let’s go through an example of a nested if-else statement in C.

#include <stdio.h>
int main()
{
int number;
printf("Please enter an integer number: ");
scanf("%d", &number);
if (number > 0)
{
printf("The entered number is positive.\n");
if (number % 2 == 0)
{
printf("The entered number is even.\n");
}
else
{
printf("The entered number is odd.\n");
}
}
else if (number < 0)
{
printf("The entered number is negative.\n");
}
else
{
printf("The entered number is zero.\n");
}
return 0;
}

Output:

Please enter an integer number: 25
The entered number is positive.
The entered number is odd.

In the above program, the entered integer number is checked for positive or negative in the first if-else block. Within this block, the nested if-else statement is used to check whether the entered number is odd or even.

Jump Statements in C

The jump statements in C are employed to modify the flow of control in a program. Three jump statements available in C are break, continue, and goto. Each of them has a unique purpose and syntax.

Syntax of Jump Statements in C

break;

continue;

goto label;
...
...

label:
statement;

Working of Jump Statements in C

The break statement either terminates the execution of the innermost loop or switch statement and shifts the control to the next statement after the switch or loop.

The continue statement skips the remaining statements inside the ongoing iteration of a loop and moves to the next iteration. It lets you bypass the execution of the remaining code in the existing iteration.

The goto statement lets you shift the control to a labelled statement inside the same function.

Examples

#include <stdio.h>
int main() 
{
int x;
printf("Here is an example of jump statements\n");
// Demonstration of break statement
printf("\nHere is an example of break statement:\n");
for (x = 1; x <= 10; x++) 
{
if (x == 5) 
{
break; // Terminates the loop when x is 5
}
printf("%d ", x);
}
// Demonstration of continue statement
printf("\n\nHere is an example of continue statement:\n");
for (x= 1; x <= 10; x++) 
{
if (x == 5) 
{
continue; // Skips the iteration when x is 5
}
printf("%d ", x);
}
// Demonstration of goto statement
printf("\n\nHere is an example of goto statement:\n");
x = 1;
loop:
if (x <= 10) 
{
printf("%d ", x);
x++;
goto loop; // Jump back to the 'loop' label
}

return 0;
}

Output:

Here is an example of jump statements

Here is an example of break statement:
1 2 3 4

Here is an example of continue statement:
1 2 3 4 6 7 8 9 10

Here is an example of goto statement:
1 2 3 4 5 6 7 8 9 10

Explanation:

  • The break statement ends the loop if the value of the variable becomes 5.

  • The continue statement skips the loop iteration if the value of the variable becomes 5.

  • The go to statement jumps back to the loop label to print values from 1 to 10.

Conclusion

The nested if else statement in C is beneficial for making complex decisions depending on multiple conditions. Nesting if-else statements inside each other allow you to develop a hierarchical structure that facilitates more accurate control flow in your C programs.

Going through tutorials is certainly a wise decision to strengthen your C fundamentals. You can pursue upGrad’s Master of Science in Computer Science at Liverpool John Moores University to considerably elevate your career in the tech industry. The course equips you with cutting-edge skills in the context of software development using languages like Java and Python. 

Enroll now to start your journey!

FAQs

1. Can you achieve multiple levels of nesting in if-else statements?

It is one of the commonly asked nested if-else statement in C questions. Yes, you can attain multiple levels of nesting in if-else statements. For the same, you can nest if-else statements within other if-else statements to develop complex decision-making structures.

2. Can you simplify or avoid using nested if-else statements in C?

In certain cases, you can simplify or avoid using nested if-else statements by using logical operators (like || for "or" condition, && for "and" condition). Restructuring the code structure or using logical operators helps you attain the same functionality with reduced levels of nesting, ultimately increasing code readability.

3. Can you use else if in a nested if-else statement?

You can use else-if statements within a nested if-else structure to analyse additional conditions. The corresponding else if statements will be evaluated if the previous conditions are false. This allows you to undergo further branching depending on multiple conditions.

4. Is it difficult to maintain nested if-else statements?

The plethoric nesting of if-else statements can make your code harder to understand and maintain. Make sure to use nesting judiciously. Moreover, you can use alternative control structures like separate functions or switch statements to enhance code maintainability and clarity.

Leave a Reply

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