Looping/Iterating
Last updated
Was this helpful?
Last updated
Was this helpful?
Repeating the same task
Doing the same thing across a data structure
for
/while
/do while
(imperative)
Format
need to ensure that loop is terminated
what the booleanExpression is for
Can make booleanExpression = true, and loop will be infinite
but have a break
in the body to exit the loop
Format -
similar to while loop, but always does one execution of the body first.
Format -
Not good for functional style as initialization has mutated variable.
initialization can declared in the loop, and has scope just for the for loop.
Can declared outside of loop, initialized in loop and thus have scope inside nad outside of loop
If declared and init outside of loop, but reinit inside for loop, error will occur
Inifinite loop for(;;) { // body }
All components in for loop are optional
Can have multiple initialization statements, booleanExpressions and updateStatements
Must have same data type in initialization
aka for-each loop
-format
collection
must implement Iterable interface
-
using a label (which is optional)
This will break out of the top level loop instead of inner loop where the break is set.
used in while/do while/switch/ for
finishes the flow of the current loop.
Uses the same syntaz as that of break when using optional lables
used in while/do while/for
stackoverflow exception
-