Java Loops Cheat Sheet

Quick reference for for/while/do-while/enhanced for loops in Java.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## For loop
Basic for loop
for (int i = 0; i < 5; i++) {
  System.out.println(i);
}

# Loop from 0..4

Loop N times
for (int i = 0; i < n; i++) {
  // ...
}
Reverse loop
for (int i = n - 1; i >= 0; i--) {
  // ...
}
## Enhanced for (for-each)
Enhanced for (for-each)
for (String s : list) {
  System.out.println(s);
}

# Iterate values without index

## While / do-while
while loop
while (condition) {
  // ...
}
do-while loop
do {
  // ...
} while (condition);

# Executes body at least once

More in Java

No other published sheets yet.

Recommended next

No recommendations yet.