Nested if with Example in Java

In this class, We discuss Nested if with Example in Java.

The reader should have prior knowledge of if elseif else statements. Click Here.

The “If” statement is written inside the “If” statement. We call it nested if.

With an example, we understand it better.

Example:

if(condition)

{

if(condition)

{

}

else

{

}

}

else

{

if(condition)

{

}

else

{

}

}

In the above example, the condition of the first if statement is true. Then the “if” statement inside is executed.

Below example helps the reader to understand the situation where a nested if statement helps.

Example:

Write a program to divide students into batches based on the below table conditions.

Student marks for five subjects will be provided.

1) Maths

2) English

3) Physics

4) Social

5) Chemistry

Total marks > 400 and maths > 90 he belongs to Bathch A.

Total marks > 400 and maths < 90 he belongs to Bathch B.

Total marks <= 400 and maths > 90. He belongs to Batch C.

Total marks <= 400 and maths < 90 he belongs to Bathch A.

The below diagram shows the complete code.

The first two conditions are the same total marks greater than 400.

The second conditions are different in the first two lines in the table.

So we go with the nested if condition here.

Code: