Java Arena
Step 2 of 5

continue

break leaves the loop. Its partner continue only skips the rest of this round: the loop goes straight back to its start and carries on. It's handy for skipping input that shouldn't be handled:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            int number = Integer.valueOf(scanner.nextLine());
            if (number == 0) {
                break;
            }
            if (number % 2 != 0) {
                System.out.println(number + " is odd, skipped");
                continue;
            }
            System.out.println("Half of " + number + " is " + number / 2);
        }
    }
}
Input
8
5
12
0
Output
Half of 8 is 4
5 is odd, skipped
Half of 12 is 6

For 5 the continue runs, so the "Half of" line is skipped for that round only. The loop then reads 12 as usual.

A loop often starts with its checks, each ending in break or continue, and then does the real work for the input that got through them.

Your task

Challenge 1 of 3

Read test scores until -1. A score above 100 is a typing mistake: print Invalid score and skip it with continue. Print every other score as Score: X. For the input 85, 120, 70 and -1 the program prints:

Score: 85
Invalid score
Score: 70

Challenge 2

Read numbers until 0. Skip negative numbers with continue, without printing anything, and print the square of every positive one. For the input 3, -2, 4 and 0 the program prints:

9
16

Challenge 3

Read ages until 0. An age below 1 or above 120 isn't a real age: print Not a real age and skip it. For the others, print how old the person will be in 10 years. For the input 25, 150, 7 and 0 the program prints:

In 10 years: 35
Not a real age
In 10 years: 17