×

java · level 1 · unit 4 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

An if runs code only when its is true.

Picture a fork in the path with a sign asking a question. An if asks a condition like that, inside round brackets. If the answer is yes, Java runs the lines inside. If not, it walks straight past.

Java answers every condition with true or false. A value that can only be one of these is a , and it can live in a box too. To ask whether two numbers match, use two equals signs, ==.

The lines that belong to the if sit in a , between curly brackets. Indent them by four spaces so your eyes can see the block too.

public class Main {
    public static void main(String[] args) {
        int goals = 3;
        System.out.println(goals == 3);
        if (goals == 3) {
            System.out.println("Hat-trick! Take a bow.");
        }
        System.out.println("Full time.");
    }
}

A boolean box can be the whole condition, like if (raining).