layout | title | permalink |
---|---|---|
page |
401.02 Reading Notes |
/401-R02/ |
-
for
: (as in JS): iterates based on set (int) variable that is adjusted through each loop.-
Example using all three (individually optional) elements:
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 (
-
Can iterate over a collection/array. Example iterating over "elementHere" in "arrayHere"
for(Type elementHere : arrayHere)
-
-
for each
: compact form of iteration, likeexampleArr.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 syntaxwhile (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
-
packages may end in wildcard
*
to include all classes of that package instead of particular classes- classes may be fully named (by directory/package) within code instead of imported
-
Importing all classes is not significant to performance, and is less error-prone due to potential naming issues caused by explicit class imports