×

java · level 1 · unit 5 · lesson 4

  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

stops a loop early. skips a turn.

Sometimes you find what you need halfway through a loop. break stops the loop straight away, and Java carries on with the first line after it.

continue is gentler. It skips the rest of this turn only, then the loop moves on to the next one, like skipping a song you do not like.

Both usually sit inside an if, so they only happen when a condition is true.

public class Main {
    public static void main(String[] args) {
        for (int song = 1; song <= 5; song++) {
            if (song == 4) {
                break;
            }
            System.out.println("Playing song " + song);
        }
        System.out.println("Party over!");
    }
}

After break or continue, the rest of the block is skipped for that turn.