QuestenaPractice that shows what to review next
Topic lesson

Loops and iteration: trace control and choose what is visited

About 5 min

Questions stay in the language in which they were published.

These questions ask how many times a loop runs, where control moves after break or continue, and whether an iteration variable receives values, indexes, or property names. Trace the loop form before looking at the data.

Choose a control-flow or iteration trace

ConstructTrace to use
for statementRun initialization once, then check the condition before each body execution. After a normally completed body, run the update and check again. For an array with length n, starting at zero and continuing while the index is less than n visits every valid index once.
while and do...whilewhile checks before the body, so it can run zero times. do...while runs the body first and checks afterward, so the first body execution does not depend on the condition.
break and continueAn unlabeled break exits the nearest loop immediately. Continue skips the rest of the current body; in a for statement, control then reaches the update before the next condition check.
for...ofConsume values from an iterable. With an array, the variable receives elements. With a string, it advances by Unicode code point, so one valid surrogate pair is consumed together.
for...inEnumerate enumerable string property names, including eligible inherited ones. On an ordinary array these include index strings rather than the element values.

When both an array index and its value are needed, array.entries() provides an iterable that yields one two-part pair at a time; pair destructuring can bind the two parts during for...of. An ordinary object does not provide the iterable protocol by default, so direct use with for...of throws a TypeError. Object.keys, Object.values, or Object.entries can first produce an iterable Array for the desired view.

Where the mistakes come from

Tempting routeHow to reject it
Reordering the for phases, making while run once, or treating break as a final updateWrite the trace explicitly. Initialization is once; pre-test loops check before entry; an executed break leaves before later work in that loop.
Using continue as another spelling of breakContinue abandons only the remaining statements of the current iteration. The loop's own next-iteration sequence still determines what happens before the next test.
Swapping for...of values with for...in property namesAsk whether the source is being consumed as an iterable or enumerated as an object's properties. On arrays, one route gives elements and the other gives index strings.
Counting UTF-16 units as separate string iterations or allowing an index equal to lengthString iteration advances by code point, while array index loops use a half-open boundary. The value equal to length is already beyond the final valid index.

Try it

Question 99

Which statements about a do...while loop are true? Select all that apply.

  1. Its body runs at least once
  2. It tests its condition before the initial body execution
  3. Its condition controls whether another iteration begins
  4. Its condition is ignored after the body runs
Question 100

A for...of loop processes the array ["red", "blue"]. What does its loop variable receive in order?

  1. The strings 0 and 1
  2. Two key-value pair arrays
  3. The array length twice
  4. The strings red and blue
Question 104

An unlabeled break executes inside the inner loop of two nested loops. What is its immediate control-flow effect?

  1. Only the rest of the current iteration is skipped
  2. Both loops always terminate
  3. The outer loop terminates while the inner loop continues
  4. The inner loop terminates
Start drillPractice this topic
Loops and iteration: trace control and choose what is visited · Questena