Lorem ipsum dolor prumus versus lorem.

Forex & Stock

Lorem ipsum dolor prumus versus lorem.

Lorem ipsum dolor prumus versus lorem.

Nested If in Java Programming

Placing If Statement inside another IF Statement is called as Nested If in Java Programming. The Java If Else statement allows us to print different statements depending upon the expression result (TRUE, FALSE). Sometimes we have to check further even when the condition is TRUE. In these situation, we can use Nested IF statements but please be careful while using it.

For example, every person is eligible to work if he is 18 years old or above else he is not eligible but companies will not give job to every person. So, we use another IF Statement (Also called as Nested If Statement) to check his education qualifications or any specific company requirements. In this article we will show you, How to write nested If statements in Java Programming language with example. Before we get into the example, let us see the syntax of Nested If in Java.

Nested If in Java Syntax

The Nested If in Java Programming language is as follows:

     //If the test condition 1 is TRUE then, it will check for test condition 2           //If the test condition 2 is TRUE, these statements will be executed           Test condition 2 True statements;           //If the test condition 2 is FALSE, these statements will be executed           Test condition 2 False statements;      //If the test condition 1 is FALSE then these statements will be executed      Test condition 1 False statements;

Nested If in Java Flow Chart

Following picture will show you the flow chart of the Java Nested If statement

  • If the Condition 1 is FALSE then STATEMENT3 will be executed.
  • If Test Condition 1 is TRUE then it will check for the Test Condition 2
    • If it is TRUE then STATEMENT1 will be executed or
    • Else STATEMENT2 is executed.

Nested If in Java Programming Example

This Java program allows user to enter his / her age and we are going to store it in the variable age. If the given age is less than 18 we are going to print 2 statements. If the condition fails we will check one more condition (Nested) and if it succeed we print something. If the nested condition fails we print some other thing.

JAVA CODE

> // Example for Nested If in Java Programming package ConditionalStatements; import java.util.Scanner; private static Scanner sc; public static void main(String[] args) {sc = new Scanner(System.in); System.out.println(" Please Enter you Age: ");System.out.println("You are Minor."); System.out.println("You are Not Eligible to Work");if (age >= 18 && age <= 60 )  {System.out.println("You are Eligible to Work");System.out.println("Please fill in your details and apply");System.out.println("You are too old to work as per the Government rules");System.out.println("Please Collect your pension!");System.out.println("\nThis Message is coming from Outside the IF ELSE STATEMENT");

ANALYSIS

If the person age is less than 18 then he is not eligible to work. If the person age is greater than or equal to 18 then first condition fails, it will check the else statement. Within the Else statement there is another if condition (this is called as Nested If).

  • Nested IF Statement will check whether the person age is greater than or equal to 18 and less than or equal to 60. If the condition is TRUE then he can apply for the job.
  • If the condition is FALSE then he is too old to work as per the government.
  • We also place one System.out.println statement outside the If Else block and it will be executed irrespective of condition result.

OUTPUT 1: From the below screenshot you can observe that, We entered the age = 16. Here first If condition is TRUE so, statements inside the first If block will be executed.

Nested If in Java Programming 1

OUTPUT 2: We are going to enter age = 25 means first IF condition is FALSE. It will go to else block and within the else block, it will check theif (age >= 18 && age <=60) , which is TRUE so it will print the statements inside this block.

OUTPUT 3: This time, we are going to enter age = 61 means first IF condition is FALSE. It will go to else block and within the else block, it will check theif (age >= 18 && age <=60) , which is FALSE that’s why it will print the statements inside Nested else block.

Thank You for Visiting Our Blog


Category: Stock

Similar articles: