Loops (for/while/do-while)

Computer Science · Java

Question 1 of 10

0

    What number does this Java code print? public class Main { public static void main(String[] args) { int sum = 0; for (int i = 1; i < 11; i += 1) { sum += i; } System.out.println(sum); } }

    Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.

    How to type your answer
    • Fractions: 3/4. Mixed numbers: 1 1/2.
    • Decimals with a point or a comma: 0.75 or 0,75.
    • Negative numbers start with a minus: -3/4.
    • Percentages need the % sign: 75%.

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Loops (for/while/do-while) explained

    A loop repeats a block of instructions. The three kinds you will meet most are the for loop, the while loop and the do-while loop. They differ in when the test that decides whether to keep going happens, and not every language has every kind. Python, for example, has no do-while loop.

    • A for loop suits a known number of repeats, or visiting every item in a collection. Its header sets the start, the stop point and the step.
    • A while loop checks its condition before each pass. If the condition is false at the start, the body never runs. Use it when the number of repeats depends on something that changes inside the loop.
    • A do-while loop checks its condition after each pass, so the body always runs at least once. Languages such as Java and C provide it.

    A common mistake is writing a while loop and forgetting to change the variable in the condition. The condition never becomes false, so the loop runs forever, which is called an infinite loop.

    Worked example

    How many times does this loop run, and what is the final value of n?

    n = 1
    count = 0
    while n < 100:
        n = n * 2
        count = count + 1
    
    1. The values of n checked at the start of each pass are 1, 2, 4, 8, 16, 32 and 64. Each is less than 100, so the body runs.
    2. After the pass with n = 64, n becomes 128. Now 128≥100128 \ge 100, so the loop stops.
    3. The body ran 7 times, so count = 7 and n = 128.

    Check: 27=1282^7 = 128, and 26=642^6 = 64 is still below 100.

    What you will practise

    New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.

    Example questions

    • Easy

      What number does this Java code print?

      public class Main { public static void main(String[] args) { int sum = 0; for (int i = 2; i < 8; i += 1) { sum += i; } System.out.println(sum); } }

    • Medium

      What number does this Java code print?

      public class Main { public static void main(String[] args) { int n = 2; int count = 0; while (n < 6) { count++; n++; } System.out.println(count); } }

    • Hard

      What number does this Java code print?

      public class Main { public static void main(String[] args) { int total = 0; for (int i = 1; i <= 3; i++) { if (i == 3) { continue; } for (int j = 1; j <= 6; j++) { if (j > i) { break; } total += j; } } System.out.println(total); } }