capypad
0 day streak
java / beginner
Snippet

Iterating with While Loops

A while loop repeatedly executes a block of code as long as a given boolean condition remains true. It is useful when the number of iterations is not known in advance.

snippet.java
java
1
2
3
4
5
int count = 1;
while (count <= 3) {
System.out.println("Count: " + count);
count++;
}
Breakdown
1
int count = 1;
Initializes a counter variable to 1.
2
while (count <= 3)
Checks if count is less than or equal to 3 before each iteration.
3
count++;
Increments the counter by 1 to eventually end the loop.