Home >Java >javaTutorial >Nested if Statements in Java

Nested if Statements in Java

WBOY
WBOYOriginal
2024-08-30 15:23:331090browse

Nested if Statement is one of the decisions making statements in Java that flows according to certain conditions. The branching of these conditions is a result of the program’s state change. That is, there will be an if-else condition inside another if-else. If, if-else, if-else-if, jump, switch-case, etc., are some of the other decision making statements in Java. Now, let us see the Nested-if Statement in detail.

ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock Tests

Syntax of Nested if Statement

Following is the syntax of Nested if Statement in Java.

If (cond1)
{
//        Executes when the cond1 is satisfied
If (cond2)
{
//        Executes when the cond2 is satisfied
}        }

Here, Cond1 is Condition 1, and Cond2 is Condition 2.

Example:

If (A1= =A2) {
Print A1 is equal to A2
If (A1= =A3) {  Print A1, A2 and A3 are equal.  }
}

Flowchart

The following figure depicts the flow chart of the Nested-if condition.

Nested if Statements in Java

Working of Nested if Statements in Java

Nested-If works similar to the normal If-else condition. The only difference is that there will be an if condition inside another if condition. The working will be as follows.

  • If Condition 1 is True, then go to if condition 2. If condition 2 is satisfied, its body will execute; otherwise, else part will execute.
  • If Condition 1 is False, then the body of the else part will be executed.
  • Once the condition check is finished, exit the loop.
  • Continue the execution of statements after the loop

 The count of the if-else condition varies depending on the user’s requirement.

Examples of Nested if Statements

In order to understand Nested-if in detail, let us see the examples using Java.

Example #1

A simple java program to implement Nested-if condition with only if conditions.

//Nested-if Java program with if conditions only
public class NestedIfExample {
public static void main(String args[]) {
//declare 2 variables and store some values in it
int num1 = 23;
int num2 = 45;
//if the number 1 is 23
if( num1 == 23 ) {
//if number is 45
if( num2 == 45 ) {
System.out.print("Number 1 is :"+ num1 +" and Number 2 is :"+ num2);
} // end of if condition 2
} //end of if condition 1
} //end of main method
} //end of class

Output:

Nested if Statements in Java

In this program, two variables- num1 and num2 are declared that stores two numbers, 23 and 45, respectively. In the if condition, num1 is checked whether it is 23. As it is true, then nested if gets executed. That is, another if condition, whether number 2 is 45, is also checked. As it is also true, a line is printed, displaying “Number 1 is 23, and Number 2 is 45”.

Example #2

A simple java program to implement Nested-if condition with both if and else conditions.

//Nested-if Java program with both if and else conditions
public class NestedIfExample {
public static void main(String args[]) {
//declare 2 variables and store some values in it
int num1 = 23;
int num2 = 48;
//if the number 1 is 23
if( num1 == 23 ) {
//if number is 45
if( num2 == 45 ) {
System.out.print("Number 1 is :"+ num1 +" and Number 2 is :"+ num2);
} // end of if condition 2
else
{
System.out.print("Number 2 is not 45");
}//end of else condition 2
} //end of if condition 1
} //end of main method
} //end of class

Output:

Nested if Statements in Java

In this program, two variables- num1 and num2 are declared that stores two numbers, 23 and 48, respectively. In the if condition, num1 is checked whether it is 23. As it is true, then nested if gets executed. That is, another if condition, whether number 2 is 45, is also checked. As it is not true, a line gets printed displaying “Number 2 is not 45”.

Example #3

A simple java program to implement a Nested-if condition that takes input from the user.

//Nested-if Java program that takes input from user and checks the condition
import java.util.Scanner;
public class NestedIfExample {
public static void main(String args[]) {
//create object of scanner
Scanner <u>sc</u>= new Scanner(System.in);
System.out.print("Enter the number to be checked: ");
int num1 = sc.nextInt();
//if the number 1 is greater than or equal to 23
if( num1 >= 23 ) {
System.out.print("Number 1 is :"+ num1 +" and it is greater than 23.");
//if number is 45
if( num1 >= 45 ) {
System.out.print("Oh!! it is greater than 45 also");
} // end of if condition 2
else
{
System.out.print(" But, the number "+num1+" is less than 45");
}//end of else condition 2
} //end of if condition 1
else
{
System.out.print("The number "+num1+" is less than 23");
}//end of else condition 2
} //end of main method
} //end of class

Output:

Nested if Statements in Java

In this program, the num1 variable is declared. Then, the user is asked to input num1. Here, 33 is given as input, and In the if condition, num1 is checked whether it is greater than or equal to 23. As it is true, then nested if it gets executed. That is, another if condition, whether number 2 is greater than or equal to 45, is also checked. As it is not true, a line is printed, displaying, “Number 1 is 33, and it is greater than 23. But the number 33 is less than 45”.

Suppose we have given input as 20. What will be the output?? Let us check how the flow will be in that case.

Nested if Statements in Java

In this case, as the first condition itself is not satisfied, the else part gets executed. That is, a line will be printed as” The number 20 is less than 23”.

Conclusion

Nested if Statement is a decision-making statement in Java containing certain branches with an if condition inside another if condition. Syntax, working, and examples of Nested-if is discussed in this document.

The above is the detailed content of Nested if Statements in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Else-If Statement in JavaNext article:Else-If Statement in Java