Lecture 4 - Repetition Flashcards
What are some disadvantages to duplicating code?
Programs become large
They also become time-consuming to write
Any corrects must be made at possibly many places …
… and there is a chance that at least one of these corrections will be made in error (i.e., as a typo).
What are repetition statements?
These are Python statements that direct the computer to repeat the execution of specific statements as often as is necessary and where we as programmers decide what “necessary” means.
What are two examples of repetition statements?
Condition-controlled loops
Sequence-controlled loops (also called count-controlled loops by the textbook).
What is a ‘while’ loop?
This is a condition-controlled loop.
As long as the condition expression is True when evaluated the statements in the while’s block are executed.
When the condition expression is False when evaluated control passes to the statement that appears below the block.
How is a ‘while’ loop written?
The condition expression is written using the same syntax as we have seen for conditional statements (i.e., if, if-else, if-elif-else)
The condition expression is boolean (i.e., evaluates to True or False)
If True, execute the block and go back to the top of the while statement and re-evaluate the condition.
If False, the while statement is finished.
How would you get a ‘while’ loop to stop executing?
Some statement(s) within the loop body must either result in the condition evaluating to False at some point in execution, or... Some statement explicitly forces the control-flow out of the loop body (the break statement).
What is an iteration?
one execution of a loop’s body
What is a ‘while’ loop also called?
a top-tested or pretest loop
What are some characteristics of the ‘while’ loop?
The condition is always evaluated before performing an iteration.
The loop body will never execute if the condition evaluates to False when the ‘while’ is encountered in control-flow.
‘while’ loop almost always require some statements to be executed prior to the loop itself!
What is an infinite loop?
a loop that has no way of stopping
The loop iterates until the program is somehow interrupted by the user.
Usually such a loop, when written, is the result of a mistake by the programmer when writing code for the ‘while’
What is a ‘for’ loop also known as?
a sequence-controlled loop
In a ‘for’ loop what is the number of times the loop iterates determined by?
The number of times the loop iterates is determined by the length of some sequence of values.
When that sequence happens to be made up consecutive integers, we can also called this a count-controlled loop.
What is a count-controlled loop?
A type of ‘for’ loop that iterates once per value in the sequence.
The actual value in the sequence for the current loop iteration can be directly used with the for-loop’s block via a target variables.
How can ‘range()’ be used?
with one integer argument: produce the sequence of numbers from zero up to but not including the argument’s value
with two integer arguments: produce the sequence of numbers from the first argument up to but not including the second argument
with three integer arguments: same as above, where the third argument is a step value
What is the main purpose of a target variable?
Refer to each item in the sequence and use the item as an operand in an arithmetic expression or as an argument for some function call.
(Like the term accumulator, target describes a particular kind of variable usage; but it is not a data type, nor is it a Python syntax construct)