Java loops

From wikinotes
Revision as of 18:13, 9 February 2019 by Will (talk | contribs) (→‎break/continue)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

for loop

// statement1:   run once before starting loop
// statement2:   while statement true, looping continues
// statement3:   execute once per loop
for (int i=0; i < 10; i++){
    // ...
}

for each loop

This operates exclusively on arrays.

String classnames = {"andrew", "will", "ryan"};
for (String name: classnames){
    System.out.println(name);
}


while loop

while (x < 10) {
    x++;
}

break/continue

  • break; ends a loop
  • continue; skips to next iteration of loop