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
Syntax
if(test_expression1) { //statements //Executes when the test_expression1 is true if(test_expression2) { //statements //Executes when the test_expression2 is true } }
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.
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).
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
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
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…
View Comments
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.
Nice article