What is Nested if Statement in Java?

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Nested if refers to an if statement within an if statement. When we write an inner if condition within an outer if condition, then it is referred to as a nested if statement in java.

Nested if is a decision-making statement that works similar to other decision-making statements such as if, else, if..else, etc. It executes a block of code if the condition written within the if statement is true. However, in the nested-if statement, the block of code is placed inside another if block. And the inner block of code will execute only when the outer condition holds true.

Therefore, we use the nested if statement to evaluate more than one condition in a program and return multiple values depending on the test condition.

An example of a nested if statement could be a person should be qualified as well as an adult to be hired in a company. Therefore, in this example, first, the condition for the person's qualification is checked, and if it is true, then only the company checks if the person is an adult or not. So, there are two conditions checked, one within another. Therefore they are nested if statements.

Syntax of Nested If Statement

The syntax for the nested if statement is as follows -

As you can see in the syntax, the inner if statement is only checked if the outer if condition is satisfied; otherwise not.

Nested If Condition

As discussed earlier, nesting refers to within. Nested if refers to if within an if statement. However, there can be any number of if statements within another if statement. The nested if statement comes under the category of the decision-making statements in Java.

Let us see the syntax for the infinite number of if statements, one within another.

However, if the outer if condition is satisfied and returns true, then only the inner if condition is checked; otherwise not. Moreover, you can also write an if..else statement within an if condition, which would be referred to as a nested if..else statement in Java.

Flowchart

Let us now understand the nested if with the help of a flowchart.

The flowchart has been created based on the below syntax -

The figure below depicts how the control in a nested if condition will flow when executed.

example of nested if syntax

Working

The nested if statement works similar to an if..else statement. The only difference is that instead of an else statement, there would be another if statement in the syntax.

We will now see a code based on which the working of the nested if statement is explained further.

Output:

Let us understand how this nested if condition works when it is executed.

  • If the test_condition1, that is, a == 97 is true, then go the test_condition2, which is z == 122.
  • If the test_condition2 is true, then execute the block of statement written inside the second if statement, which will print "Correct" as the output.
  • However, if the test_condition2 is false, then the else part is executed if present in the program, otherwise, it will exit the loop. Here, in the above code, it is present, therefore, "Wrong" will be printed as the output.
  • If the test_condition1 is false, then the corresponding outer else part is executed, if present, otherwise it will also exit the loop.

Examples

Implement Nested-If Condition With Only If Conditions

Now we will see an example where the nested if conditions are implemented only using the if condition.

In this example, we are given two numbers, x and y. These nummbers are to be checked if they are equal with 30 and 40 respectively based on which certain statement is printed.

Let us see the code for the same.

Output:

As you can see in the above code, first, the condition x == 30 is checked, and if satisfied, checks for the next condition y == 50, which when satisfied, prints the subsequent statements in the if block.

Nested-If Condition With Both If and Else Conditions

Now let us see the nested if condition using both the if and else conditions in a program.

In this example as well, we are given two numbers, x and y. These numbers are to be checked if they are equal with 30 and 40 respectively based on which certain statement is printed. However, the difference lies with the introduction of an else block in the program.

Let us see the code for the same.

Output:

As you can see in the above code, first, the condition x== 30 is checked. If this condition is true, then only the nested if..else condition is checked further. However, if the first condition is true then the next if condition y == 40 is checked. If it is true, then the if block is executed; otherwise, the statements of the else block are executed.

The Nested-If Condition That Takes Input From the User

Now let us see the nested if conditions that take the input from the user.

In this example, we will find the right vaccine to be injected into a particular person. The conditions for getting a vaccine are that a person must be above 18 years of age, after which different genders will be provided with different kinds of vaccines in the market.

In our problem, we will be taking the age and the gender of a person as the input from the user.

Let us see the code for the same.

Output:

The output "CovidShield" came when the gender and age variables are provided with the input 'M' and 30.

Therefore, as you can see in the above code, first of all, the inputs are taken from the user, after which if the person is above 18 years old then only they are provided with a different type of vaccine according to their gender.

First of all, if the gender is 'M', that is, a male, then they are provided with "CovidShield vaccine". However, if the gender is 'F', that is, female, they are injected with "Covaxine" whereas the rest all the genders have been provided with the "Sputnik" dose.

Conclusion

  • Nested if statement is a decision-making statement that is used when we have to make decisions based on certain conditions.
  • The nested if statement in Java is a set of if conditions one within another.
  • The inner if conditions are only executed when the outer if condition results in true; otherwise the corresponding else block is executed.
  • The nested if condition can also be used with nested if..else statement.