A common task is processing every number in a range. Maybe you need to sum all integers from to , or check each number between and for a property.
The for loop maps directly to ranges:
// Sum from 1 to 100
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
System.out.println(sum);
Pay attention to the boundaries. Using i <= 100 includes in the range. Using i < 100 stops at . Getting this wrong by one number is the most common loop bug, and you'll see it come up again in the off-by-one unit.
If you need to iterate backwards over a range, flip the setup: start high, use >=, and decrement with i--.