Java Tutorials
Java Methods
Java Classes
Java File Handling
Java supports common logical conditions from mathematics
a < b
a <= b
a > b
a >= b
a == b
a != b
You can use these terms to perform different actions in different decisions.
Java has the following conditional statements:
if
to specify a block of code to be executed, if a specified condition is trueelse
to specify a block of code to be executed, if the same condition is falseelse if
to specify a new condition to test, if the first condition is falseswitch
to specify many alternative blocks of code to be executedUse the statement if
you want to specify a Java code block to use if the condition is true
.
if (condition) {
// block of code to be executed if the condition is true
}
Note that if
is written in small capital letters. Capitalized characters (If or IF) will create an error.
In the example below, we check two numbers to see if 20 is greater than 18. If the situation is true
, print some text:
if (20 > 18) {
System.out.println("20 is greater than 18");
}
We can also check the variables:
int x = 20;
int y = 18;
if (x > y) {
System.out.println("x is greater than y");
}
In the example above we use two variables, x and y, to test whether x is greater than y (using >
operator). Since x is 20, and y is 18, and we know that 20 is greater than 18, we print on the screen that "x is greater than y".
Use else
statement to specify a code block to use if the situation is false
.
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
int time = 20;
if (time < 18) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
// Outputs "Good evening."
In the example above, the time (20) is greater than 18, so the situation is false
. As a result, we move on to else
condition and print on the "Good evening" screen. If the time was less than 18, the program would print "Good Day".
Use else if
the statement specifies the new condition if the original condition is false
.
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
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."
In the example above, the time (22) is greater than 10, so the initial condition is false
. The next situation, in else
case the statement, is also false, so we move on to another situation as condition1 and condition2 are both false
- and print on the "Hello" screen.
However, if the time was 14, our program would print "Good Day."
There is also a short-hand alternative, known as a ternary operator because it contains three operands. It can be used instead of multiple lines of code in one line. It is often used for a simple replacement if other statements:
variable = (condition) ? expressionTrue : expressionFalse;
Instead of writing:
int time = 20;
if (time < 18) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
You can easily write:
int time = 20;
String result = (time < 18) ? "Good day." : "Good evening.";
System.out.println(result);