C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Else: The else-statement here catches all cases not yet matched. The value 2, unmatched, ends up in the else-statement.
Tip: We call the System.out.println method, with a String argument, to display results to the console.
ConsoleComparison: To compare a number, we use 2 equals signs. This is an expression. It evaluates to a true or false result.
Java program that uses if-statement
public class Program {
public static void main(String[] args) {
// Loop through three numbers.
for (int i = 0; i <= 2; i++) {
if (i == 0) {
System.out.println("Zero");
} else if (i == 1) {
System.out.println("One");
} else {
System.out.println("Else reached");
}
}
}
}
Output
Zero
One
Else reached
Tip: Sometimes it is easier to negate an entire expression. This can be read as "if not."
Java program that uses not equals
public class Program {
public static void main(String[] args) {
int value = 5;
// This expression...
if (value != 6) {
System.out.println("Not 6!");
}
// Is the same as this one.
if (!(value == 6)) {
System.out.println("Not 6!");
}
}
}
Output
Not 6!
Not 6!
And: With ||, only one expression must be true. After the first true evaluation, nothing further is tested.
Java program that uses and, or
public class Program {
public static void main(String[] args) {
int width = 10;
int height = 5;
// Both expressions must evaluate to true.
if (width == 10 && height == 5) {
System.out.println("width 10 and height 5");
}
// Only one expression must be true.
if (width == 100 || height > 0) {
System.out.println("width 100 or height greater than 0");
}
}
}
Output
width 10 and height 5
width 100 or height greater than 0
Note: Imagine the "fits" boolean was tested many times. It would make the program simpler.
Note 2: If a time-consuming method call is part of an if, storing its result in a bool can help reduce calls (and improve performance).
Java program that uses boolean
public class Program {
public static void main(String[] args) {
int width = 10;
int height = 15;
int weight = 200;
// Use a boolean to store computed result.
boolean fits = width <= 10 && height <= 20;
// We can use the boolean, not a complex expression.
if (fits && weight <= 150) {
System.out.println("It fits");
} else {
System.out.println("Does not fit");
}
}
}
Output
Does not fit
Exclamation: This means "not." So using !occupied will evaluate to true only if "occupied" is set to false.
Java program that uses true, false
public class Program {
public static void main(String[] args) {
boolean vacant = true;
boolean occupied = false;
// Test boolean variables.
if (vacant && !occupied) {
System.out.println(true);
}
}
}
Output
true
Java program that uses method in if-statement
public class Program {
static boolean isOdd(int value) {
// See if number is not evenly divisible by 2.
return (value % 2) != 0;
}
public static void main(String[] args) {
int number = 5;
// Call method in if-statement.
if (isOdd(number)) {
System.out.println(true);
}
}
}
Output
true
Here: We test a String against the literals "tea" and "java." The String equals "java" so evaluation continues until "java" is tested.
Version 1: This is the unoptimized version. It tests "tea" before "java" so two comparisons always occur.
Version 2: This version tests the most common condition first. So it incurs half as many total checks.
Tip: Check most common conditions first. We use heuristics, or frequency analysis, to optimize if-statements.
Java program that times if-statement reordering
public class Program {
public static void main(String[] args) {
String value = "java";
int count = 0;
long t1 = System.currentTimeMillis();
// Version 1: if-statements ordered most common last.
for (int i = 0; i < 100000000; i++) {
if (value == "tea") {
count++;
} else if (value == "java") {
count += 2;
}
if (count == 0) {
System.out.println(false);
}
}
long t2 = System.currentTimeMillis();
// Version 2: if-statements ordered most common first.
for (int i = 0; i < 100000000; i++) {
if (value == "java") {
count += 2;
} else if (value == "tea") {
count++;
}
if (count == 0) {
System.out.println(false);
}
}
long t3 = System.currentTimeMillis();
// ... Benchmark times.
System.out.println(t2 - t1);
System.out.println(t3 - t2);
}
}
Output
35 ms: unoptimized order
32 ms: optimized order
Java program that causes type mismatch
public class Program {
public static void main(String[] args) {
int value = 1;
// This does not compile: we must have a boolean expression.
if (value) {
System.out.println(1);
}
}
}
Output
Exception in thread "main" java.lang.Error:
Unresolved compilation problem:
Type mismatch: cannot convert from int to boolean...