- On
- By
- 2 Comments
- Categories: nested if
Nested if statement in Java language
Nested if statement in Java language
Here, we will discuss Nested if statement in Java language
In the Java programming language, the Nested if statements use if -else statements inside one or more if or else statements. One or more conditions are evaluated by the nested if statements
Declaration
Syntax
if(test_expression1) { //statements //Executes when the test_expression1 is true if(test_expression2) { //statements //Executes when the test_expression2 is true } }
flow diagram of nested if in Java
Initially, test expression of outer if is evaluated. when it is true, the body of statements are executed and then enters inner if.
Then statements of inner if are evaluated. If it is true, the body of statements is executed. If it is false, the flow of control exits from the loop.
The explanation for nested if in Java
Flow diagram for Nested if-else in Java
It is a flow chart for nested if in Java.
Here, we can see two if statements and with their else parts.
When starting the loop circulation, initially flow of control enters the outer if
Then Boolean expression is evaluated. If it is true, statements inside the body of if (outer if) are executed and flow of control enters the body of inner if.
If the Boolean expression is evaluated to be false, statements inside the body of if (outer if) are skipped from execution. The flow of control jumps to its else part for execution(outer else).
How to work nested if in Java
if(test_expression1){ //outer if System.out.println("When outer if statement is true, executes this statement"); if(test_expression2){//inner if System.out.println("When inner if statement is true, executes this statement"); } else{ //inner else System.out.println("When inner if statement is false, executes this statement"); } } else{ //outer else System.out.println("When outer if statement is false, executes this statement"); }
Program 1
public class Nested_if_java{ public static void main(String args[]){ int age=20; double gpa=1.7; if(age>=20){ if(gpa>=1.1){ System.out.print("You are selected for university"); } } } }
When we execute the above program, it will produce the following results.
You are selected for university
Program 2
class nifjava{ public static void main(String args[]){ int age=20; //assign the variable int marks=73; //assign the variable if(age>=18) { //Outer if - for check age System.out.println("Age is enough then check your marks"); //if boolean expression is true, print this statements //if boolean expression is false, go to its else part if(marks<=74) {// if outer if are true //then enter inner if for check marks System.out.println("Marks is enough"); System.out.println("So,You enter your course"); //if boolean expression is false, go to its else part } else{ System.out.println("Sorry, marks is not enough"); }//when the boolean expression of inner if is false //display this statements } else{ System.out.println("Sorry,age is not enough"); }//when the boolean expression of outer if is false //display this statements } }
When we execute the above program, it will be produced the following result
Age is enough then check your marks Marks is enough So,You enter your course
Program 3
class nifjavapro{ public static void main(String args[]){ int age=18; int marks=76; double gpa=1.5; if(age>=18) { if(marks>=75) { if(gpa>=1.00){ System.out.println("well, you can enter university"); } else{ System.out.println("Sorry, you can not enter university"); } } else{ System.out.println("Sorry,marks is not enough"); } } else{ System.out.println("Sorry,age is not enough"); } } }
When we execute the above program, it will produce the following result
well, you can enter university
Similar post
If statement in Python language
Suggested for you
Nested for loop in C++ language
Nested while loop in C++ language
Nested for loop in Java language
Nested while loop in Java language
Three dim Array in C++ language
Single dim Array in Java language
Two dim Array in Java language
Three dim Array in Java language
Single dim Array in C language
2 Comments
Lizzette Jingst December 20, 2019 at 8:53 pm
You have made some really good points there. I checked on the web for more information about the issue and found most people will go along with your views on this site.
Pyth May 21, 2021 at 3:57 pm
Nice article