java / beginner
Snippet
Iterating with For Loops
A 'for' loop is used when you know exactly how many times you want to repeat a block of code. It consists of initialization, condition, and increment.
snippet.java
1
2
3
for (int i = 1; i <= 3; i++) {System.out.println("Iteration: " + i);}
Breakdown
1
int i = 1;
Initializes the loop counter 'i' at 1.
2
i <= 3;
The loop continues as long as 'i' is less than or equal to 3.
3
i++
Increases the value of 'i' by 1 after each loop iteration.