for
: (as in JS): iterates based on set (int) variable that is adjusted through each loop.
for (int i = 0; i < 10; i++)
Can be broken with break
(named loop may be specified)
Can be named using the following syntax, where “nameHere” is the desired name: nameHere: for (
for(Type elementHere : arrayHere)
for each
: compact form of iteration, like exampleArr.forEach(exampleElement -> System.out.println(exampleElement));
while
: Defines a condition, then repeats code block until condition is not true or loop is explicitly broken (break
). Uses syntax while (conditionHere) {codeBlockHere}
do-while
: Subset of “while” loop that iterates once before checking condition. Syntax:
do {
codeToDo;
} while (condition);
Recall: Package names in Java are the directory name
When declared, package must be declared first, followed by imports
*
to include all classes of that package instead of particular classes