Loops¶
A loop is a sequence of instructions that is repeated until a certain condition is reached.
Examples¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
For Loops¶
for
loops are used to loop through a block of code a number of times
1 2 3 |
|
i
starts out at 1
and the loop will stop before i
is 6.
Every time the code block is executed i
gets incremented by 1.
The loop will execute 5 times: when i
is 1
, 2
, 3
, 4
and 5
.
While Loops¶
while
loops are used to loop through a block of code while a specified condition is true
1 2 3 |
|
i
starts out at 0
and the loop will stop when i
is smaller than 6
Every time the code block is executed i
gets incremented by 1.
The loop will execute 6 times: when i
is 0
, 1
, 2
, 3
, 4
and 5
.
Break & Continue¶
The break
statement breaks the loop and continues executing the code after the loop (if any)
The continue
statement breaks one iteration in the loop, and continues with the next iteration in the loop.