Java Revision Sheet – Loops Comparison (for, while, do-while)
Description:
This revision handout compares all three loop types in Java. It's useful for students revising
for tests or understanding when to use which loop based on real examples.
🔁 Why Use Loops?
Loops help us execute a block of code repeatedly, either a specific number of times or until a
condition is met.
🔄 Types of Loops in Java
Loop Type Best Used When… Syntax Overview
for loop Number of iterations is known for (init; condition; update) { }
while loop Iteration until condition is true while (condition) { }
do-while Execute at least once, then check do { } while (condition);
🔧 1. for Loop Example
java
CopyEdit
for (int i = 1; i <= 5; i++) {
System.out.println("Count: " + i);
}
🔄 2. while Loop Example
java
CopyEdit
int i = 1;
while (i <= 5) {
System.out.println("Count: " + i);
i++;
}
🔂 3. do-while Loop Example
java
CopyEdit
int i = 1;
do {
System.out.println("Count: " + i);
i++;
} while (i <= 5);
⚖️Comparison Table
Feature for while do-while
Entry Control? Yes Yes No (Exit control)
Runs at least once? No No Yes
Common Use Counting loops User input / condition-based Menus, retries
🧠 Exercise: Print Even Numbers from 1 to 20 Using All Loops
Try using for, while, and do-while separately.
Use if (i % 2 == 0) condition inside the loop.