The if statement
The if statement allows you to execute a certain block of code only if a certain condition is true. It is defined as this template:
if ( condition ) {
body
}
where
- condition is a boolean expression;
- body is zero or more statements;
- The if statement executes the code in body if and only if condition evaluates to true.
The following code prints a warning message if a player's health goes below a certain threshold.
if (health < healthWarningThreshold) {
System.out.println("Careful -- your health is dangerously low.");
}