For loops and counters
What you will learn
A for loop repeats a block of instructions while a condition remains true. Use it when you can describe the work with a counter or a clear sequence of steps, such as printing five numbered tickets.
public class Main { public static void main(String[] args) { for (int i = 1; i <= 3; i++) { System.out.println("Ticket " + i); } }}Expected output:
Ticket 1Ticket 2Ticket 3The counter starts at one and includes three. At four the condition fails.
How it works
The usual form is for (int i = 0; i < count; i++) { ... }. Initialization happens once. The condition is checked before each iteration, and the update runs after normal completion of the body. An iteration is one execution of that body. For count = 3, the condition sees i = 0, 1, 2 and finally 3; only the first three values enter the body. A counter declared in the header is local to the loop. The condition must be boolean (or Boolean with unboxing). Java permits omitted header parts, but use explicit bounds while learning. To build a total, initialize an accumulator before the loop and add each contribution inside it. A nested loop starts its inner counter again for each outer iteration.
public class Main { public static void main(String[] args) { int total = 0; for (int day = 1; day <= 4; day++) { int boxes = day * 2; total += boxes; } System.out.println(total); }}Expected output:
20The running totals are 2, 6, 12 and 20. The accumulator survives each iteration because it was declared before the loop.
Check a boundary or unusual input
public class Main { public static void main(String[] args) { for (int i = 3; i > 0; i--) { System.out.println(i); } int count = 0; for (int i = 0; i < count; i++) { System.out.println("Work"); } System.out.println("Done"); }}Expected output:
321DoneThe descending counter stops at zero. The second loop never enters its body because its initial condition is false.
Common mistakes
Off-by-one errors occur when the starting value and boundary disagree. Starting at zero and using i <= count executes count + 1 times for a small nonnegative count. Starting at zero with i < count executes exactly count times. Do not accidentally add a semicolon after the header; that creates an empty loop body. Avoid updating the same counter both in the header and in the body unless the algorithm deliberately requires it.
Try it yourself
For an integer n from 0 to 20, calculate the total cost of n boxes at 6 credits each using a for loop and an accumulator. Print only the total. Try n = 4, 0, 1 and 20. Do not replace the loop with multiplication in this practice task.
Save each example independently as Main.java. Compile with javac Main.java and run with java Main. Local Java practice only; automatic ByteMind Java grading is not established.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
For loops and counters — knowledge check
Apply for loops and counters to examples and boundary conditions.
How many times does for (int i = 0; i < 4; i++) execute its body?
When does the initialization run?
Where should a running total be declared?
Which condition prints 3, 2, 1 with i initially 3 and i--?
Can a variable declared as int i in the for header be used after that loop?