Loops help repeat actions easily. Initialization, condition, and update control how many times the loop runs.
0
0
Loop initialization, condition, update in Java
Introduction
When you want to count from 1 to 10 and do something each time.
When you need to process each item in a list one by one.
When you want to repeat a task until a certain number is reached.
When you want to update a value step by step inside a loop.
Syntax
Java
for (initialization; condition; update) { // code to repeat }
Initialization runs once at the start.
Condition is checked before each loop; if false, loop stops.
Update runs after each loop cycle to change variables.
Examples
This prints numbers 0 to 4.
i starts at 0, runs while less than 5, and increases by 1 each time.Java
for (int i = 0; i < 5; i++) { System.out.println(i); }
This counts down from 10 to 1, decreasing
count by 1 each loop.Java
for (int count = 10; count > 0; count--) { System.out.println(count); }
This prints even numbers from 2 to 10 by adding 2 each time.
Java
for (int x = 2; x <= 10; x += 2) { System.out.println(x); }
Sample Program
This program counts from 1 to 5. It starts i at 1, checks if it is less or equal to 5, prints the count, then adds 1 to i.
Java
public class LoopExample { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { System.out.println("Count: " + i); } } }
OutputSuccess
Important Notes
Initialization happens only once at the start of the loop.
If the condition is false at the start, the loop body will not run at all.
Update changes the loop variable to eventually stop the loop.
Summary
Loops repeat code using three parts: initialization, condition, and update.
Initialization sets the starting point.
Condition decides if the loop continues.
Update changes the variable to move the loop forward.