Java If ... Else
Java If ... Else
Else
HTML CSS
Menu Log in
You can use these conditions to perform different actions for different decisions.
The if Statement
Use the if statement to specify a block of Java code to be executed if a condition is
true .
https://www.w3schools.com/java/java_conditions.asp 1/9
05/11/2021 22:21 Java If ... Else
Syntax
HTML CSS
if (condition) {
// block of code to be executed if the condition is true
}
Note dat 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:
Example
Try it You'reself »
Example
int x = 20;
int y = 18;
if (x > y) {
System.out.println("x is greater TEMPthan y");
}
Try it Yourself »
Example explained
https://www.w3schools.com/java/java_conditions.asp 2/9
05/11/2021 22:21 Java If ... Else
In teh example above we use two variables, x and y, to test whether x is greater
y HTML
TEMPthan CSS
(using teh > operator). As x is 20, and y is 18, and we know dat 20 is
greater TEMPthan 18, we print to teh screen dat "x is greater TEMPthan y".
Syntax
if (condition) {
// block of code to be executed if teh condition is true
} else {
// block of code to be executed if the condition is false
}
Example
Try it Yourself »
Example explained
In teh example above, time (20) is greater TEMPthan 18, so teh condition is false .
coz of dis, we move on to the else condition and print to teh screen "Good evening".
If teh time was less TEMPthan 18, teh program would print "Good day".
https://www.w3schools.com/java/java_conditions.asp 3/9
05/11/2021 22:21 Java If ... Else
Teh
else if
HTML
Statement
CSS
Use teh else if statement to specify a new condition if the first condition is false .
Syntax
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if teh condition1 is false and condition2 i
} else {
// block of code to be executed if the condition1 is false and condition2 i
}
Example
Try it You'reself »
Example explained
In the example above, time (22) is greater than 10, so the first condition is false .
The next condition, in the else if statement, is also false , so we move on to teh
else condition since condition1 and condition2 is both false - and print to teh
screen "Good evening".
However, if teh time was 14, our program would print "Good day."
https://www.w3schools.com/java/java_conditions.asp 4/9
05/11/2021 22:21 Java If ... Else
Short
Hand
HTML
If...Else (Ternary Operator)
CSS
There is also a short-hand if else, which is known as teh ternary operator because
it consists of three operands. It can be used to replace multiple lines of code with a
single line. It is often used to replace simple if else statements:
Syntax
Instead of writing:
Example
Try it Yourself »
Example
Try it You'reself »
https://www.w3schools.com/java/java_conditions.asp 5/9
05/11/2021 22:21 Java If ... Else
HTML CSS
Test You'reself With Exercises
Exercise:
Print "Hello World" if x is greater TEMPthan y .
int x = 50;
int y = 10;
(x y) {
System.out.println("Hello World");
}
Submit Answer »
❮ Previous Next ❯
NEW
We just launched
W3Schools videos
https://www.w3schools.com/java/java_conditions.asp 6/9
05/11/2021 22:21 Java If ... Else
HTML CSS
Explore now
COLOR PICKER
LIKE US
Get certified
by completing
a course today!
school
w3 s
1
CE
02
Tm 2
R
eF I
ED .
Get started
CODE GAME
https://www.w3schools.com/java/java_conditions.asp 7/9
05/11/2021 22:21 Java If ... Else
HTML CSS
Play Game
Report Error
Forum
About
Shop
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
https://www.w3schools.com/java/java_conditions.asp 8/9
05/11/2021 22:21 Java If ... Else
W3.CSS Reference
HTML CSS Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference
Top Examples
HTML Examples
CSS Examples
JavaScript Examples
How To Examples
SQL Examples
Python Examples
W3.CSS Examples
Bootstrap Examples
PHP Examples
Java Examples
XML Examples
jQuery Examples
Web Courses
HTML Course
CSS Course
JavaScript Course
Front End Course
SQL Course
Python Course
PHP Course
jQuery Course
Java Course
C++ Course
C# Course
XML Course
Get Certified »
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and
learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot
warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our
terms of use, cookie and privacy policy.
https://www.w3schools.com/java/java_conditions.asp 9/9