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

Nested if statements of Java

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

Flow diagram-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

The syntax of nested if in Java

 

Explanation Nested if in Java

 

Flow diagram for Nested if-else in Java

Nested if 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

 

 

Suggested for you

if statement in Java

if statement in C

if statement in CPP

if statement in Python

Nested if in Python programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

View Comments

Share
Published by
Karmehavannan

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago