typestar

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

  1. for drives an index; for-each visits every element without one.
  2. continue skips one iteration, break leaves the loop entirely.
  3. while loops on a condition the body must eventually change.

Keywords and builtins used here

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.

Type this snippet

Step 3 of 3 in Flow control, step 10 of 29 in Language basics.

← Previous Next →

Loops in other languages