Chapter 5-Loops Flashcards
What does a loop do?
A loop can be used to tell a program to execute statements repeatedly.
What does a while loop do?
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
Why is indentation important in the while loop (and all loops for that matter?) ?
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.
What is a while loop?
A condition-controlled loop; it is controlled by true/false conditions
What is a for loop?
A count-controlled loop, which repeats a specified number of times.