← Back to Week 2 Hub

Looping Exercises

Three small programs to practice while, do/while, and for loops

Workbook 2a, p.63

What You're Building

Three separate programs, each using a different loop type. Program 1 (WhileLoop): Use a while loop to print "I love Java!" five times. Program 2 (DoWhileLoop): Do the same thing using a do/while loop. Program 3 (ForLoop): Use a for loop to count down from 10 to 1, then print "Launch!" with a 1-second delay between each number.

Example Run
Program 1 — WhileLoop
I love Java! I love Java! I love Java! I love Java! I love Java!
Program 2 — DoWhileLoop
I love Java! I love Java! I love Java! I love Java! I love Java!
Program 3 — ForLoop (countdown)
10... 9... 8... 7... 6... 5... 4... 3... 2... 1... Launch!
Key detail: The ForLoop countdown should have a 1-second pause between each number. Look up Thread.sleep() — it takes milliseconds, so 1 second = 1000.
Concepts You'll Use

This exercise reinforces these concepts from Week 2:

Flow Diagram

Each program follows a similar pattern but uses a different loop structure:

Program 1 WhileLoop Counter + while condition
Logic Print 5 times Check condition first
Program 2 DoWhileLoop Counter + do/while
Logic Print 5 times Run body first, then check
Program 3 ForLoop Start at 10, go to 1
Logic Print number + delay 1-second pause each time
After Loop Print "Launch!" Runs once after loop ends

Programs 1 and 2 produce identical output. The difference is in the loop structure, not the result.

← CellPhone Service 3 Roll the Dice →