What is a loop?
A loop repeats a block of instructions. It is useful for processing many values, repeating actions, or examining combinations of data.
Study 05 Loops and Repetition with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What is a loop?
A loop repeats a block of instructions. It is useful for processing many values, repeating actions, or examining combinations of data.
What four parts does a loop normally have?
Initialization establishes the starting state, the condition determines whether to continue, the body performs repeated work, and progress updates the state toward stopping.
When is a while loop appropriate?
A while loop repeats as long as its condition is true. It is appropriate when the number of repetitions depends on input or changing state.
When is a for loop usually the clearest choice?
A for loop processes each item in a sequence or iterable. It is usually clearest for handling every item in a collection.
What values does range(5) produce?
range(5) produces 0, 1, 2, 3, 4. The ending value is excluded.
Why use enumerate() in a for loop?
enumerate() supplies both an item’s position and its value, avoiding the need to maintain an index manually.
What is a counter?
A counter records how many times an event occurs. It is commonly initialized to zero and increased only when the event’s condition is satisfied.
What is an accumulator?
An accumulator combines many values into one result. Initialize it with the identity value: 0 for addition or 1 for multiplication.
What does break do?
break immediately terminates the innermost for or while loop. It is useful when the desired result has been found or further work is unnecessary.
What does continue do?
continue skips the rest of the current iteration and begins the next one. In a while loop, skipping an essential update can cause nontermination.
When does Python’s loop else clause run?
A loop else clause runs when the loop finishes normally, but not when break ends it. This supports searches where else reports that no match was found.
What is a nested loop?
A nested loop is a loop inside another loop. The inner loop completes all its iterations for each iteration of the outer loop.