Chapter 5-Loops Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What does a loop do?

A

A loop can be used to tell a program to execute statements repeatedly.

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

What does a while loop do?

A

A while loop executes statements repeatedly as long as the condition remains true. If the condition does not remain true, then python will exit the loop

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

Why is indentation important in the while loop (and all loops for that matter?) ?

A

Without indentation in the while loop, we may be left with an infinite loop where the continuation condition is always true. For example:

sum=0
i=1
while i<10:
    sum=sum+i
i=i+1

the i=i+1 does not update the loop because it is not indented inside the loop and therefore the loop is infinite.

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

What is a while loop?

A

A condition-controlled loop; it is controlled by true/false conditions

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

What is a for loop?

A

A count-controlled loop, which repeats a specified number of times.

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