Java If ... Else
Java Conditions and If Statements
Conditions and if statements let you control the flow of your program - deciding which code runs, and which code is skipped.
Think of it like real life: If it rains, take an umbrella. Otherwise, do nothing.
Every if
statement needs a condition that results in
true
or false
.
This means if
statements work hand-in-hand with
boolean
values:
Example
boolean isRaining = true;
if (isRaining) {
System.out.println("Bring an umbrella!");
}
Most often, conditions are created using comparison operators, like the ones below:
- Less than: a < b
- Less than or equal to: a <= b
- Greater than: a > b
- Greater than or equal to: a >= b
- Equal to: a == b
- Not equal to: a != b
You can use these conditions to perform different actions for different decisions.
Java has the following conditional statements:
- Use
if
to specify a block of code to be executed, if a specified condition is true - Use
else
to specify a block of code to be executed, if the same condition is false - Use
else if
to specify a new condition to test, if the first condition is false - Use
switch
to specify many alternative blocks of code to be executed
The if Statement
Use the if
statement to specify a block of Java code to be executed if a condition is true
.
Syntax
if (condition) {
// block of code to be executed if the condition is true
}
The condition inside the if
statement must result in a
boolean
value - it can be either a boolean expression
(like x > y
) or a boolean variable (like isLightOn
).
Also note that if
is in lowercase letters. Uppercase letters (If or IF) will generate an error.
In the example below, we test two values to find out if 20 is greater than 18. If the condition is true
, print some text:
We can also test variables:
Example
int x = 20;
int y = 18;
if (x > y) {
System.out.println("x is greater than y");
}
Example explained
In the example above we use two variables, x and y,
to test whether x is greater than y
(using the >
operator). As x is 20, and y is 18, and we know that 20 is greater than 18, we print to the screen that "x is greater than y".
Comparison is also often used to check if two values are equal, using the ==
operator:
Example
int x = 20;
int y = 20;
if (x == y) {
System.out.println("x is equal to y");
}
Here the condition x == y
is true, because both x
and y
are 20, so the message "x is equal to y" is printed.
Using Boolean Variables
You can also test boolean variables directly in an if
statement:
Example
boolean isLightOn = true;
if (isLightOn) {
System.out.println("The light is on.");
}
Note: Writing if (isLightOn)
is the same as writing if (isLightOn == true)
, but shorter and easier to read.
Here is the same example with the value false
to see that the program continues even when the code block does not run:
Example
boolean isLightOn = false;
if (isLightOn) {
System.out.println("The light is on."); // This will not be printed
}
System.out.println("This line always runs.");
In the next chapters, you will learn how to run code when the condition is false
(using else
), or when there are several conditions to check
(using else if
).