Loops : while, for Flashcards
1
Q
While Loops
A
- A while loop causes one or more statements to execute as long as, or while, a condition evaluates to True.
- Use a while loop when it is not known how many times to repeat a loop.
- A while loop has a condition that is followed by a body which is indented. If the while loop’s condition evaluates to True then the body of the loop is executed.
General form:
while condition:
body
- When the bottom of the loop body is reached, execution returns to the top of the loop, and the loop condition is evaluated again.
- If the condition still evaluates to True then the body of the loop executes for a second time, execution once again returns to the top of the loop.
- The loop’s body continues to execute until the while loop condition evaluates to False. When this occurs, the loop’s body is skipped, and execution continues at the first statement after the body of the while loop.
2
Q
For Loops
A