
While loop (programming)

Upgrade for more content
True or false? If you want to repeat code, you can use a loop.
Lena has written a program for her robot so that it can give Leon and Maria their favourite chocolate. And it does that well. Leon starts the robot. It picks up a piece of chocolate, looks at it and sees it is milk chocolate and checks if there are nuts in it. No.
No nuts. Therefore, Maria gets the piece of chocolate. Leon didn’t get any chocolate. So, he restarts the program. The robot picks up a new piece of chocolate.
This time, it is dark chocolate. The robot gives that chocolate to Leon. “I can’t be bothered to restart the program again.” “No. If we have to restart the robot, we could just as easily take the chocolates ourselves.” Maria and Leon don’t want to restart the program time after time. How can they avoid doing that? Maria wants to improve Lena’s program.
So, first, she needs to look at how the pseudo code is written. The instructions are: pick up a piece of chocolate and give it to Maria or to Leon or to throw the chocolate away. The instructions are only executed once, but Maria wants the program to start over and go in a loop. Her requirement is that while there are chocolates in the box, the program should be repeated: iterated. Maria’s requirement is the condition in the loop.
When the condition is true, the instructions are iterated. When the condition is false, the program continues in the code. This is a 'while' loop. Maria writes that the program should ask a question to the user: How many chocolates are in the box? The answer is noted in the variable called chocolates.
The requirement ‘while there are chocolates in the box’ is the same as the variable ‘chocolates is greater than 0’. If the instructions in the loop are performed, a piece of chocolate will be removed. Then, a new value for the chocolates variable is required. So, Maria puts in a row at the bottom of the loop. Chocolates equals chocolates minus 1.
Now, it’s a bit difficult to see which rows belong to the loop and are intended to be repeated. How can it be clearer in the code? Well, Maria indents the lines like this. Now, Maria is going to test the program. She starts... ...and inputs there are 10 chocolates.
Then, the program tests the condition in the while loop. The variable chocolates equals 10. It’s greater than 0. The condition is true. If the condition is true, the instructions are executed in the loop.
The robot picks up a chocolate. It’s milk chocolate. So, the first condition in the loop is true. Next condition: Are there nuts? No nuts.
Then, the instruction will not be followed to throw away the chocolate. Instead, the program goes to: ‘else: give Maria chocolate’. Now, the number of chocolate pieces is less than before. And the variable chocolates gets the value 10 minus 1, which is 9. The program has executed all the instructions in the loop and returns to the top of the loop.
Is the condition still true? Yes, chocolates is 9. It is greater than 0. The instructions in the loop are executed again. The robot picks up a new chocolate.
And chocolates decreases by 1 to 8. The program tests the condition again. 8 is greater than 0 and the condition is true and then the robot continues. It iterates the loop as long as the condition is true. But when the box is empty, then the number of chocolates is 0.
What then? 0 is not greater than 0. So, the condition is false and the robot doesn’t perform the instructions in the loop. The program goes on, but there are no rows after the loop. The program ends.
With the help of the new loop, Leon and Maria can get a lot of chocolate from the robot without having to restart the program. That’s good! Right, Leon and Maria?