Lecture 4 - Repetition Flashcards

1
Q

What are some disadvantages to duplicating code?

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are repetition statements?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are two examples of repetition statements?

A

Condition-controlled loops

Sequence-controlled loops (also called count-controlled loops by the textbook).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a ‘while’ loop?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How is a ‘while’ loop written?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How would you get a ‘while’ loop to stop executing?

A
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).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is an iteration?

A

one execution of a loop’s body

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a ‘while’ loop also called?

A

a top-tested or pretest loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are some characteristics of the ‘while’ loop?

A

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!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is an infinite loop?

A

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’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a ‘for’ loop also known as?

A

a sequence-controlled loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

In a ‘for’ loop what is the number of times the loop iterates determined by?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a count-controlled loop?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How can ‘range()’ be used?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the main purpose of a target variable?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

cont.

A

cont.