Loops allow you control over repetitive steps you need to do in your control flow. Python has two different kinds of loops we will talk about: while loops and for loops. Either one can be used interchangeably; but, as you will see there are couple cases where using one over the other makes more sense.
The primary purpose of loops is to avoid having lots of repetitive code.
Loop through a block of code (the body) WHILE a condition is true.
while (condition_is_true):
# execute the code statements
# in the loop body
pass
See the code below. In this case, we start with a simple counter in x = 1. Then, after the loop starts, it checks to see if x < 6, and 1 is less than 6, so the loop body gets executed. We print out 1 and then increment x. Then we go to the top of the loop and check to see if x (now 2) is less than 6. Since that’s true so we print out 2 and increment x again. This continues like this for three more times, printing 3, 4, and 5.
Then, x is incremented to 6, and the check is made again, 6 < 6 … well, no that is false. So we don’t execute the loop’s body and we fall through to the last print line, and print out x.
x = 1
while (x < 6):
print(x)
x += 1
print("ending at", x) # ? what will print here ?While loops work well in situations where the condition you are testing at the top of the loop is one that may not be related to a simple number.
while (player[1].isAlive() == True):
player[1].takeTurn()
game.updateStatus(player[1])
This will keep letting player[1] take a turn in the game until the player dies. Another way to do something like this is with an infinite loop. (No, infinite loops are not necessarily a bad thing, watch.) We’re going to use both continue and break in this example, and we will describe them better after we’re done with loops.
player = game.newPlayer()
while (true): # <- notice right here, an infinite loop
player.takeTurn()
game.updateScores()
game.advanceTime()
if (player.isAlive() == true):
continue # start at top of loop again.
else:
break # breaks out of loop and ends game.
game.sayToHuman("Game Over!")
Here, we are using the continue statement to force the flow of control to the top of the loop if the player is still alive after the 'take turn' code. We are also using the break statement to break out of the infinite loop when the player dies, letting us do other things after the player has 'died'.