Unit 8: Operators & Loops
Loops & Iteration in Javascript
Loops
- Repeated actions with beginning and ending points determined by statements:
- for
- do…while
- while
- labeled
- break
- continue
- for…in
- for…of
for
Statements
- Loop until condition evaluates as
false
.
- Anatomy:
for
, followed by two/three paranthetical expressions separated by semicolons, then a statement. Like:
- `for (let a = 200; a > previousvariablehere; a = a / 2)
- let cutstatus = ‘halved’ `
- The initial expression: first paranthetical. This executes, and can declare variables
- The condition expression: second paranthetical. This is evaluated, and if true loop executes. If false,
for
terminates (note:w/out condition statement, executes as if true)
- The statement: executes. Can group multiple in the same curled brackets
- The increment expression: executes if present
- Loop returns to condition expression
while
Statements
- Executes as long as condition evaluates true
- Anatomy:
while
, followed by paranthetical condition, then statement. Like:
`while (condition)
statement’
- The condition is tested before execution. If true, statement executes and condition is tested again. If false, loop terminates and passes to statement
- Multiple statements can be blocked together in curly brackets
- Note: make sure condition eventually returns
false
– otherwise loop is infinite