Skip to content

Latest commit

 

History

History
39 lines (25 loc) · 1.51 KB

401-02.md

File metadata and controls

39 lines (25 loc) · 1.51 KB
layout title permalink
page
401.02 Reading Notes
/401-R02/

Arrays, Loops, & Imports

Loops

Types of Loops in Java

  • 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, 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);

Importing packages

  • 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