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

 

 

Similar post

If statement in Java language

If statement in C++ language

If statement in C language

If statement in Python language

Nested if in Java language

Nested if in C language

Nested if in C++ language

Nested if in Python language

 

Suggested for you

for loop in C++ language

While loop in C++ language

Nested for loop in C++ language

Nested while loop in C++ language

New keyword in Java language

For loop in Java language

While loop in Java language

Nested for loop in Java language

Nested while loop in Java language

Two dim Array in C++ 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

Two dim Array in C language

Three dim Array in C language

 

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

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

1 month ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

1 month ago