Iterative Statements
Iterative statements are used when you need to keep repeating statements until the condition for termination is not met. These types of statements are normally exhibited in:
1) for loops
For loops are used for definite loops where the number of iterations is known.
A for loop starts with an initialization followed by a condition and increment/decrement.
Consider a simple for loop that prints numbers from 1 to 10.

The FizzBuzz problem as explained on our AlgoDaily Youtube channel(https://youtu.be/giS2G3TCmWI) is a classic example of using for loops to solve a problem.
1public class fizzbuzz {
2
3 public static void main(String[] args) {
4 for (int i = 1; i <= 100; i++) {
5
6 if (i % 3 == 0 && i % 5 == 0) {
7
8 System.out.println("FizzBuzz");
9 } else if (i % 3 == 0) {
10
11 System.out.println("Fizz");
12 } else if (i % 5 == 0) {
13
14 System.out.println("Buzz");
15
16 } else {
17
18 System.out.println(i);
19 }
20
21 }
22 }
23}
In this program, the statements are repeated until the conditions are met. The fizz buzz program details what numbers are divisible by 3, 5, and both 3 and 5. However, as aforementioned, see a more detailed explanation from the AlgoDaily youtube channel.
2) While loops
While loops are used when the number of iterations is not known.
In contrast to for loops, there is no built-in loop control variable.
1public class Example {
2
3 public static void main(String[] args) {
4 int i = 0;
5
6 while (i < 11) {
7
8 System.out.println(i);
9 i++;
10
11 }
12
13 }
14
15}
The above example shows the condition specified within the loop. While the number i
is less than 11 then the print statement is continuously executed. The i
is used as a built in control variable.
3) Do while loops
- Do while loops follow a similar structure as while loops however the statement is executed first before evaluating the condition.
In the example attached, we are printing the value of the i
and incrementing first. Then check the condition in the while looking to see if the number i
is less than 11.