Thursday, September 4, 2025
HomeLanguagesJavaNested if in Java

Nested if in Java

Generally, if condition works like yes or no type. If the condition satisfies, it executes some block of code. Otherwise, it doesn’t execute the code. Let us see the syntax of the simple if condition.

Syntax : 

if( condition ){

       statements ;
       
}

Nested if condition 

Nested means within. Nested if condition means if-within-if. Nested if condition comes under decision-making statement in Java. There could be infinite if conditions inside an if condition. The below syntax represents the Nested if condition.

Syntax : 

if( condition ){

      if( condition ){
      
                if( condition ){
                
                         ......
                }
       }
}

Note – If the inner condition satisfies then only outer if will be executed. Along with if conditions we can write else conditions also.

Example 1 

Java




import java.util.*;
import java.lang.*;
import java.io.*;
  
class GFG
{  
    public static void main(String args[])
    {
        int a=10;
          int b=20;
       
        if(a==10){
            if(b==20){
                System.out.println("Lazyroar");
            }
        }
    }
}


Output

Lazyroar

Code explanation :

  • In the first step, we have imported the required packages.
  • In the next step, we have created a class names GFG
  • In the next step, we have written the main method.
  • Within the main method, we have assigned values to variables.
  • Using nested if conditions, we have printed a statement.
  • Here two statements are true. Hence statement was executed successfully.

Example 2 :

Java




import java.util.*;
import java.lang.*;
  
class GFG
{  
    public static void main(String args[])
    {
        int a=10;
          int b=20;
        
        if(a==10){
  
            if(b!=20){
                System.out.println("Lazyroar");
            }
            
            else{
                System.out.println("GFG");
            }
        }
    }
}


Output

GFG

Code explanation :

  • In the first step, we have imported the required packages.
  • In the next step, we have created a class names GFG
  • In the next step, we have written the main method.
  • Within the main method, we have assigned values to variables.
  • Using nested if conditions, we have printed a statement.
  • Here inner if the condition is not true. Hence else part is executed.

Nested if condition comes under decision-making statement in Java. It contains several branches with an if condition inside another if condition. The syntax, code examples, and explanations of Nested-if statements are covered in detail in the above article.

RELATED ARTICLES

Most Popular

Dominic
32260 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6625 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6746 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6694 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS