java / beginner
Snippet
Looping with Boolean Logic
A while loop continues to execute as long as its condition evaluates to true. In this case, we use a boolean flag to control when the loop should stop.
snippet.java
1
2
3
4
boolean active = true;while (active) {active = false;}
Breakdown
1
boolean active = true;
Initializes a boolean variable to serve as the loop condition.
2
while (active) {
Check if 'active' is true; if so, enter the loop block.
3
active = false;
Updates the flag to false so the loop will exit after this iteration.