Easy Tutorial
❮ Arrays Upperbound Java9 Improved Javadocs ❯

Java Conditional Statements - if...else

An if statement contains a boolean expression and one or more statements.

Syntax

The syntax for an if statement is as follows:

if(boolean expression)
{
   // Statements to be executed if the boolean expression is true
}

If the value of the boolean expression is true, the code block inside the if statement is executed; otherwise, the code following the if block is executed.

Test.java File Code:

public class Test {

   public static void main(String args[]){
      int x = 10;

      if( x < 20 ){
         System.out.print("This is the if statement");
      }
   }
}

The above code compiles and runs with the following result:

This is the if statement

if...else Statement

An if statement can be followed by an else statement, which executes when the boolean expression in the if statement is false.

Syntax

The syntax for if…else is as follows:

if(boolean expression){
   // Executes if the boolean expression is true
}else{
   // Executes if the boolean expression is false
}

Example

Test.java File Code:

public class Test {

   public static void main(String args[]){
      int x = 30;

      if( x < 20 ){
         System.out.print("This is the if statement");
      }else{
         System.out.print("This is the else statement");
      }
   }
}

The above code compiles and runs with the following result:

This is the else statement

if...else if...else Statement

An if statement can be followed by one or more else if...else statements, which can test multiple conditions.

When using if, else if, else statements, note the following:

Syntax

The syntax for if...else is as follows:

if(boolean expression 1){
   // Executes if boolean expression 1 is true
}else if(boolean expression 2){
   // Executes if boolean expression 2 is true
}else if(boolean expression 3){
   // Executes if boolean expression 3 is true
}else {
   // Executes if none of the above boolean expressions are true
}

Example

Test.java File Code:

public class Test {
   public static void main(String args[]){
      int x = 30;

      if( x == 10 ){
         System.out.print("Value of X is 10");
      }else if( x == 20 ){
         System.out.print("Value of X is 20");
      }else if( x == 30 ){
         System.out.print("Value of X is 30");
      }else{
         System.out.print("This is the else statement");
      }
   }
}

The above code compiles and runs with the following result:

Value of X is 30

Nested if…else Statement

Using nested if…else statements is legal. This means you can use an if or else if statement inside another if or else if statement.

Syntax

The syntax for nested if…else is as follows:

if(boolean expression 1){
   // Executes if boolean expression 1 is true
   if(boolean expression 2){
      // Executes if boolean expression 2 is true
   }
}

You can nest else if...else in the same way as an if statement.

Example

Test.java File Code:

public class Test {

   public static void main(String args[]){
      int x = 30;
      int y = 10;

      if( x == 30 ){
         if( y == 10 ){
             System.out.print("X = 30 and Y = 10");
          }
       }
   }
}

The above code compiles and runs with the following result:

X = 30 and Y = 10
if( x == 30 ){
    if( y == 10 ){
        System.out.print("X = 30 and Y = 10");
    }
}

The above code compiles and runs with the following result:

X = 30 and Y = 10
❮ Arrays Upperbound Java9 Improved Javadocs ❯