Unlike the perulangan for where you know when it starts and when it ends, a while loop uses a boolean condition as its controller.
So what makes the while loop worth choosing over the for loop? When should you use a while loop? And how do you build one? Let's go further into it.
Want the
whileloop concept explained without writing any code? Take a look at the explanation of Control Structure Concepts in Programming.
Structure of the WHILE Loop in Python
In general the structure of a while loop is simple enough, and the same goes for other programming languages. Here is the structure in Python:
while <kondisi>:
<perintah>
With:
kondisi: a boolean expression that will be evaluated as the loop controllerperintah: one or several commands that will run
How Does the WHILE Loop Work?
If the for loop has its number of iterations set in advance, the while loop doesn't need you to declare one. Instead, you use a boolean condition as the controller of the loop.
A while loop keeps running while the condition holds True, and it stops only when the condition is False or a break command is called.
Try running the Python code below:
i = 1
while i <= 5:
print(i)
i += 1
If you run it, the numbers 1 through 5 come out. What is actually happening in that code? Let's go through it one step at a time.
- At first the variable
iholds 1. Then you enter thewhilestatement, which evaluates whether the current value ofiis less than or equal to 5. Because1 <= 5, the condition isTrue, so the commands on lines 3 and 4 run. On line 3 you print the value ofi, and on line 4 you add 1 toi, soinow holds 2. - Once every command in the loop has run, the flow returns to the
whilecondition check. Nowiholds 2, so the condition is stillTrueand the commands run again. The value ofiis printed and incremented by 1 until it holds 3. - This carries on until
iholds 6. Wheniis 6 the condition becomesFalse, because6 <= 5is not true. At that point the commands inside the loop don't run and the loop is done.
Now what if the code is changed to this:
i = 10
while i <= 5:
print(i)
i += 1
Run it and nothing comes out at all. That's because the very first while check, 10 <= 5, is False, so the loop is finished before it even starts.
WHILE Loop and Infinite Loop
As you know, this while loop is controlled by a boolean condition. So what happens if the condition is always True? For this experiment, you'll modify the earlier code into this:
i = 1
while i <= 5:
print(i)
Just delete i += 1, then run it and see what happens.
Pretty soon the number 1 is printed an enormous number of times. Cool, right ๐. How long will it keep printing? Forever, hahaha.
Relax. To stop it, just press ctrl + c, or close the whole program while you're at it, hehe.
This is what people call an infinite loop, or a loop without a limit. The cause is a while condition that is always True, namely 1 <= 5.
An infinite loop can turn into a serious problem when it happens unexpectedly. A loop that never ends makes the program stop executing other code, because it is busy working on the loop.
Even so, some cases do require an infinite loop, for example when you build a game.
WHILE Loop and List
Python has a quirk: if you pass an empty list ([]) as a boolean value, it is automatically evaluated as False. You can put that to use as the condition of a while loop.
Let's look at the example code below:
buah = ["apel", "pisang", "jeruk", "semangka", "anggur"]
while buah:
print("Aku mau makan", buah.pop())
If you run it, the output will look like this:
Aku mau makan anggur
Aku mau makan semangka
Aku mau makan jeruk
Aku mau makan pisang
Aku mau makan apel
Why does it work like that? Because you call the pop() method on the list buah on every iteration. In the end the list buah becomes an empty list.
Closing
That's all for this one, and I hope it's useful ๐.
Next time you'll look at the break and continue commands in Python 3.
See ya.. ๐