Loops in Java
for counts, for-each walks, while runs until told otherwise.
for (int i = 0; i < 3; i++) {
System.out.println("for: " + i);
}
var langs = new String[] { "Java", "Kotlin", "Scala" };
for (var lang : langs) {
if (lang.equals("Scala")) continue;
System.out.println("for-each: " + lang);
}
int n = 1;
while (n < 100) {
n *= 3;
if (n > 50) break;
}
System.out.println("stopped at " + n);
How it works
fordrives an index; for-each visits every element without one.continueskips one iteration,breakleaves the loop entirely.whileloops on a condition the body must eventually change.
Keywords and builtins used here
breakcontinueforifintnewvarwhile
The run, in numbers
- Lines
- 16
- Characters to type
- 323
- Tokens
- 125
- Three-star pace
- 95 tpm
At the three-star pace of 95 tokens a minute, this run takes about 79 seconds.
Step 3 of 3 in Flow control, step 10 of 29 in Language basics.