← All lessons
Java basics · Lesson 5 of 10

Making decisions

So far every line has run every time. Real programs make choices: if the password is right, let them in; otherwise, don't. A choice starts with a condition, a question whose answer is true or false.

Java
int age = 15;
System.out.println(age > 12);
System.out.println(age < 10);
System.out.println(age == 15);
System.out.println(age != 15);
System.out.println(age >= 15);
Prints
true
false
true
false
true

The comparisons are <, >, <= (at most), >= (at least), == (is equal to) and != (is not equal to). Watch the difference: one = stores a value, two == ask whether two values are equal. (For strings, remember to ask with .equals.)

if and else

if runs a block of code only when its condition is true. The condition goes in round brackets and the block in curly ones. else gives the block to run otherwise, and else if adds more questions in between. Java checks them top to bottom and runs only the first block whose condition is true.

Java
int temp = 18;
if (temp > 25) {
    System.out.println("Hot");
} else if (temp > 15) {
    System.out.println("Nice");
} else {
    System.out.println("Cold");
}
System.out.println("Done");
Prints
Nice
Done

18 isn't over 25, so the first block is skipped; it is over 15, so Nice prints and the rest of the chain is skipped. Done is outside the chain and always runs. Indent the lines inside each block so you can see what belongs to it; Java goes by the brackets, but people go by the indentation.

Combining conditions

&& means and (both must be true), || means or (at least one must be true), and ! means not (flips true and false).

Java
int age = 15;
boolean hasTicket = true;
if (age >= 12 && hasTicket) {
    System.out.println("Come in");
}
if (age < 5 || age > 65) {
    System.out.println("Free entry");
} else {
    System.out.println("Pay at the door");
}
System.out.println(!hasTicket);
Prints
Come in
Pay at the door
false

switch

When you're comparing one value against a list of exact options, switch is neater than a long else if chain. Each case lists the values that match, -> says what to do, and default catches everything else. A switch can also give back a value:

Java
int day = 6;
String name = switch (day) {
    case 1 -> "Monday";
    case 6, 7 -> "Weekend";
    default -> "Midweek";
};
System.out.println(name);
Prints
Weekend

Test the edges. If the rule is "12 and over", try 11, 12 and 13. Most bugs in conditions are a > that should have been >=.

Practice

What does this print?

int x = 7;
if (x > 5) {
    System.out.println("big");
} else {
    System.out.println("small");
}
More info

Quick reference

< > <= >=Less, more, at most, at least.
== / !=Equal / not equal (numbers, true/false and chars; strings use equals).
if (cond) { } else if (cond) { } else { }Runs the first block whose condition is true.
&&And: both must be true.
||Or: at least one must be true.
!Not: flips true and false.
switch (x) { case a -> ...; default -> ...; }Pick by exact value; can give back a value.

Related courses