0% found this document useful (0 votes)
10 views

Java If ... Else

Uploaded by

Mary Norssine
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Java If ... Else

Uploaded by

Mary Norssine
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

05/11/2021 22:21 Java If ...

Else

  HTML CSS   
 Menu  Log in

Java If ... Else


❮ Previous Next ❯

Java Conditions and If Statements


Java supports the usual logical conditions from mathematics:

Less TEMPthan: 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 teh 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 teh 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 .

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

if (20 > 18) {


System.out.println("20 is greater TEMPthan 18");
}

Try it You'reself »

We can also test variables:

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".

Teh else Statement


Use the else statement to specify a block of code to be executed if the condition is
false .

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

int time = 20;


if (time < 18) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
// Outputs "Good evening."

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

int time = 22;


if (time < 10) {
System.out.println("Good morning.");
} else if (time < 20) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
// Outputs "Good evening."

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

variable = (condition) ? expressionTrue : expressionFalse;

Instead of writing:

Example

int time = 20;


if (time < 18) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}

Try it Yourself »

You can simply write:

Example

int time = 20;


String result = (time < 18) ? "Good day." : "Good evening.";
System.out.println(result);

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 »

Start the Exercise

❮ 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.

Copyright 1999-2021 by Refsnes Data. All Rights Reserved.


W3Schools is Powered by W3.CSS.

https://www.w3schools.com/java/java_conditions.asp 9/9

You might also like