Java if-else

Summary: in this tutorial, you will learn how to use the Java if-else statement to execute a block of code if a condition is true and another code block if the condition is false.

Introduction to the Java if-else statement

The Java if-else statement evaluates a condition. If the condition is true, it executes a code block that follows the if keyword. If the condition is false, it executes another code block that follows the else keyword.

Here’s the syntax of the if-else statement:

if (condition) {
    // Code to execute if the condition is true
} else {
    // Code to execute if the condition is false
}Code language: Java (java)

In this syntax:

  • if: This keyword marks the beginning of the if-else statement.
  • (condition) : This is a condition that the if-else statement will evaluate. It can be true or false.
  • {} This is the first pair of curly brace containing the code block that the if-else statement will execute if the condition is true.
  • else: This else keyword introduces the alternate code block to be executed if the condition is false.
  • {}: The second pair of curly braces enclose the block of code that the if-else statement will execute if the condition is false.

Java if-else statement examples

Let’s take some examples of using the Java if-else statement.

1) Simple Java if-else example

The following example shows how to use the if-else statement to display different messages based on the value of a variable temperature:

public class App {
    public static void main(String[] args) {
        int temperature = 25;

        if (temperature > 30) {
            System.out.println("It's hot outside.");
        } else {
            System.out.println("It's not too hot.");
        }

    }
}Code language: Java (java)

In this example, we have an integer variable temperature with a value of 25. The if-else statement checks if the temperature is greater than 30.

Since this condition is false, the if-else statement executes the code block in the else part that displays the message "It's not too hot.".

2) Using the if-else statement to find the max of two numbers

The following uses the if-else statement to find the maximum number of two numbers:

public class App {
    public static void main(String[] args) {
        int x = 10, y = 20;
        int max;

        // find the max
        if (x > y) {
            max = x;
        } else {
            max = y;
        }

        // display the max
        System.out.println(max);

    }
}Code language: Java (java)

Output:

20Code language: Java (java)

How it works.

First, declare two integer variables x and y, and initialize their values to 10 and 20 respectively.

Second, declare the max variable that holds the maximum number.

Third, check if x is greater than y , and assign the x to the max if the condition is true. Otherwise, assign the value of y to max.

Finally, display the value of the max variable, which is 20 in this case.

Nest if-else statement

Like the if statement, you can nest an if-else statement inside an if or if-else statement.

In theory, you can have an unlimited level of nestings. But in practice, you should avoid nesting the if-else statement to make the code easier to understand.

Here’s the syntax for using nested if-else statements:

if(condition1) {
   // Code to execute if the condition1 is true
   if(condition2) {
      // Code to execute if the condition2 is true
   } else {
      // Code to execute if the condition2 is false
   }
} else {
   // Code to execute if the condition2 is false
   if(condition3) {
      // Code to execute if the condition3 is true
   } else {
      // Code to execute if the condition3 is false
   }
}Code language: Java (java)

The following example illustrates how to use the nested if-else statement to find the maximum number of three numbers:

public class App {
    public static void main(String[] args) {
        int x = 10, y = 20, z = 30;
        int max;

        if (x > y) {
            if (x > z) {
                max = x;
            } else {
                max = z;
            }
        } else {
            if (y > z) {
                max = y;
            } else {
                max = z;
            }
        }
        System.out.println(max);
    }
}Code language: Java (java)

How it works.

  • First, define three integer variables x, y, and z and initialize their values to 10, 20, and 30 respectively.
  • Second, use nested if-else statements to find the maximum value:
    • It checks if x is greater than y.
    • If yes, it further checks if x is greater than z. If true, x is assigned to max, otherwise z is assigned to max.
    • If x is not greater than y, it checks if y is greater than z. If true, y is assigned to max, otherwise z is assigned to max.
  • Finally, print the value of max, which represents the maximum among x, y, and z. In this case, it will print 30, as z is the maximum value.

Summary

  • Use Java if-else statement to execute a block of code if a condition is true or another block of code if the condition is false.
  • Avoid nesting too many if-else statements to make your code more readable and easier to understand.