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 theif-elsestatement.(condition): This is aconditionthat the if-else statement will evaluate. It can betrueorfalse.{}This is the first pair of curly brace containing the code block that theif-elsestatement will execute if the condition istrue.else: Thiselsekeyword introduces the alternate code block to be executed if theconditionisfalse.- {}: The second pair of curly braces enclose the block of code that the
if-elsestatement will execute if theconditionisfalse.
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, andzand initialize their values to 10, 20, and 30 respectively. - Second, use nested
if-elsestatements to find the maximum value:- It checks if
xis greater thany. - If yes, it further checks if
xis greater thanz. If true,xis assigned tomax, otherwisezis assigned tomax. - If
xis not greater thany, it checks ifyis greater thanz. If true,yis assigned tomax, otherwisezis assigned tomax.
- It checks if
- Finally, print the value of
max, which represents the maximum amongx,y, andz. In this case, it will print 30, aszis the maximum value.
Summary
- Use Java
if-elsestatement to execute a block of code if a condition istrueor another block of code if the condition isfalse. - Avoid nesting too many
if-elsestatements to make your code more readable and easier to understand.