What is iteration?
Iteration is the repeated execution of a block of statements. A loop is the control structure that performs iteration.
Study 4 Loops and Repetition with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What is iteration?
Iteration is the repeated execution of a block of statements. A loop is the control structure that performs iteration.
What three parts make a loop reliable?
Initialization establishes the starting state, the condition determines whether another iteration occurs, and progress or update changes state toward termination.
When should you use a for loop?
Use a for loop when the number of repetitions or the range of values is known or easy to describe.
What is the execution rule for while?
A while loop tests its condition before the body runs, so the body may execute zero times.
Why use a do...while loop?
A do...while loop tests its condition after the body, so the body always executes at least once.
What does a counter pattern do?
A counter records how many times an event occurs. It changes only when the desired event occurs, unlike the loop-control variable, which may change every iteration.
What is an accumulator?
An accumulator combines many values into one result. A sum usually starts at 0, while a product usually starts at 1.
How does the search pattern work?
A search loop examines values until it finds a match or reaches the end. A sentinel such as -1 can represent “not found,” and break can stop the search after a match.
What does a filtering loop do?
A filtering loop processes or collects only values that satisfy a condition. For example, it can add temperatures less than or equal to 100 to a safe-temperature list.
How do break and continue differ?
break terminates the nearest enclosing loop. continue skips the rest of the current iteration and moves to the next one.
When is for...of preferable for arrays?
A for...of loop directly supplies each element of an iterable, such as an array. Use an indexed for loop when the element’s index is needed.
How does a nested loop execute?
For each outer-loop iteration, the inner loop completes all of its iterations. In the example, 3 rows multiplied by 4 columns gives 12 executions of the inner body.