Summary: in this tutorial, you will learn how to use the Java if
statement to evaluate a boolean expression and execute a block of code if the expression is true.
Introduction to the Java if statement
The if
statement is a basic control flow construct in Java. The if
statement evaluates a condition and executes a block of code if the condition is true
.
Here’s the syntax of the if
statement:
if(condition) {
// code to execute if the condition is true
}
Code language: Java (java)
In this syntax:
- The
if
is the keyword that marks the beginning of theif
statement. - The
condition
located within the parentheses()
is a boolean expression that evaluates totrue
orfalse
. - The curly braces
{}
enclose the block of code to be executed if thecondition
istrue
.
But if the condition
is false
, Java will skip the code block and continue with the next statement after the if
statement.
Note that the curly brace {}
is optional if you have one statement to execute. However, it’s a good practice to always include the curly braces even if you have a single statement.
Java if statement examples
Let’s take some examples of using Java if
statement.
1) A simple Java if statement example
The following example illustrates how to use the if
statement:
public class App {
public static void main(String[] args) {
int temperature = 38;
if (temperature > 32) {
System.out.println("It's hot!");
}
}
}
Code language: Java (java)
Output:
It's hot!
Code language: Java (java)
In this example, we have an integer variable temperature
with a value of 38
.
The if
statement checks if the temperature
is greater than 32
. Because this condition is true
, the program will display the message "It's hot!"
.
2) Using the if statement with a relational operator ==
The following example uses an if
statement to check if a number is an even number using an arithmetic operator %
and a relational operator ==
:
public class App {
public static void main(String[] args) {
int number = 20;
if(number % 2 == 0){
System.out.println("The number is even.");
}
}
}
Code language: Java (java)
Output:
The number is even.
Code language: Java (java)
In this example, we have an integer variable number
with a value of 20
.
Because the expression number % 2
returns the remainder of the division, the whole expression number % 2 == 0
is true
if the remainder is zero or false
otherwise.
In other words, if the condition number % 2 == 0
is true
, the number
is an even number. The program displays the message "The number is even."
because the condition is true
.
3) Using the if statement with a logical operator
The following example uses an if
statement with the logical operator AND (&&
) to check if a person can drive:
public class App {
public static void main(String[] args) {
byte age = 17;
boolean hasDrivingLicense = true;
if (age >= 16 && hasDrivingLicense) {
System.out.println("You can drive.");
}
}
}
Code language: Java (java)
In this example, we have a variable age
with a value of 17
and a variable hasDrivingLicense
with the value of true
.
The condition age >= 16 && hasDrivingLicense
returns true
if the age
is greater than 16
and hasDrivingLicense
is true.
Because the condition is true
, the program displays the message "You can drive."
.
Nested if statement
Java allows you to nest an if
statement inside another if
statement like this:
if (condition1) {
if(condition2) {
// ...
}
}
Code language: Java (java)
In this syntax, we have an outer if
statement containing an inner if
statement.
If condition1
is true
, Java will execute the code block enclosed within the curly braces {}
. If condition1
is false
, Java will skip the code block and the program continues to the next statement after the outer if
statement.
The inner if
statement checks another condition condition2
. If the condition2
is true, Java executes the code block inside the curly braces of the inner if
statement.
In other words, the code block inside the inner if
statement only executes if both condition1
and condition2
are true.
The following example illustrates how to use a nested if statement:
public class App {
public static void main(String[] args) {
byte age = 17;
boolean hasDrivingLicense = true;
if (age >= 16) {
System.out.println("You are of legal age.");
if (hasDrivingLicense) {
System.out.println("You can drive.");
}
}
}
}
Code language: Java (java)
How it works
- The outer
if
statement checks ifage
is greater than or equal to18
(legal age). If it’strue
, it prints"You are of legal age"
and proceeds to the innerif
statement. - The inner
if
statement checks ifhasDrivingLicense
istrue
(the person has a driver’s license). - If both conditions are met, it prints
"You can drive."
But ifage
is less than18
orhasLicense
isfalse
, the innerif
statement is skipped.
In practice, you should avoid using nested if statement because it makes the code harder to read and maintain.
Summary
- Use Java
if
statement to execute a block of code if a condition is true.