When can a while loop execute zero times?
A while loop tests its condition before each iteration, so its body can execute zero times.
Study 4 Repetition and Looping with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
When can a while loop execute zero times?
A while loop tests its condition before each iteration, so its body can execute zero times.
What guarantees that a do-while loop runs once?
A do-while loop executes its body before testing the condition, so it always runs at least once.
What punctuation follows a do-while condition?
The semicolon terminates the do-while statement: do { /* body */ } while (condition);
What is the execution order of a for loop?
A for loop initializes once, tests before each iteration, runs the body, updates, and then tests again.
When is a for loop a natural choice?
Use a for loop when a counter or another regular progression naturally describes the repetition.
What does break do inside a loop?
break immediately exits the nearest enclosing loop and continues with the first statement after that loop.
What does continue do in a for loop?
continue skips the rest of the current iteration and begins the next one. In a for loop, the update runs first.
How many times can a nested-loop body execute?
A nested loop's body executes once for each outer iteration combined with each inner iteration: r times c times, or r×c.
What is the structure of an accumulation loop?
Initialize an accumulator before the loop, then update it once per visited value, such as total += value.
How does a loop count matching values?
A counting loop increments a counter whenever an item satisfies a condition, such as value > 0.
What is a sentinel-controlled loop?
A sentinel-controlled loop stops when a special value appears; in the example, -1 ends input and is not added to the total.
Which index condition avoids an array off-by-one error?
For a zero-based array with size elements, use i < size because valid indices are 0 through size - 1.