Loops allow you to create a piece of code which will repeat itself
EXAMPLE: Dispensing Cash: In an ATM, a loop must execute the action of dispensing $20 bill 5 times when a customer needs $100 in $20 bills
As the customer needs $100 in $20 bills, the condition is "till $100 has been dispensed"
The loop (action of dispensing one $20 bill) will continue until the condition ($100) has been met
When a loop is executed once, it is referred to as an one iteration
In the ATM example, there were 5 iterations
The image to the left shows a graphical representation of how a loop works. Typically, the loop keeps running (executing) as long as the condition is TRUE. In the ATM example, once $100 has been dispensed, the condition becomes FALSE and the loop stops executing.
When a loop is executed once, it is referred to as an one iteration
Generally, there are three types of loops
• For Loop
• While Loop
• Do-While Loop
While the condition is not met, the clock continues to move.
Please input a number from 1 - 12.
The hour hand will move to the hour specified.
The number entered in the box above determines the number of iterations.
If the number entered is 5, the clock hand moves to indicate 5 o' clock.
A 'for' loop is used to create code that keeps repeating until a condition is met.
The three boxes below stand for 'start', 'stop', and 'step.' The default step increment for Python is "increases by 1." In the following example, you can adjust it if you'd like.
In the Python code below, initalize the variable i to go from 0 to 5 by 1 and note that the loop stops when the condition-check is true.
I would like to review again.