Iteration
Loops (iteration) repeat code.
A while loop repeats as long as a condition is true.
For loop structure:
for (initialization; condition; update) {
// code to repeat
}
The initialization runs once at the start of the loop (commonly something like int i = 0)
The condition is checked before every loop. It must be true to continue.
The update runs after every iteration of the loop.
For loop example:
for(int i = 0; i < 5; i++){
System.out.println(i);
}
This loop starts at 0 and prints numbers up to 4.
While loop example:
int i = 0;
while(i < 5){
System.out.println(i);
i++;
}
This loop continues running while the condition is true and increments each time.
Nested loops are loops within loops.
Nested loop example:
for(int i = 0; i < 3; i++){
for(int j = 0; j < 2; j++){
System.out.println(i + "," + j);
}
}
The inner loop runs completely for each iteration of the outer loop.