Python Coding for Kids: Learn to Program your Own Games and Apps through Self-Development by Lukes Harvey

Python Coding for Kids: Learn to Program your Own Games and Apps through Self-Development by Lukes Harvey

Author:Lukes, Harvey [Lukes, Harvey]
Language: eng
Format: epub
Published: 2020-08-18T16:00:00+00:00


The ‘While’ Loop

We said earlier that ‘for’ loops are excellent when we know the number of times we need to run a block of code. But what do we do when we don’t know? This is where the “while” loops come in.

The ‘while’ loop is the most versatile technique in programming that can be used to solve a lot of problems. The way it works is simple. Just think of a sentence in English using the word “while” and you’ll realize how this loop works. Here’s an example: “I can learn a lot while reading this book” or “While I have money in my pocket, I can buy candy”. While the condition is true (while you have money), you’ll keep running in a loop buying candy. When you run out of money, you’ll exit that loop and stop buying candy because you can’t. That’s how the ‘while’ loop works in programming as well.

A ‘while’ loop doesn’t work like a ‘for’ loop, in the sense that it doesn’t need a range condition. Instead, it uses a loop condition. In other words, it relies on a Boolean that can either be true or false. So, while you have money (if true), then you’ll buy candy. If that condition is false, the rest of the code inside the loop block is ignored and your program continues with the next block. Here’s a simple example.

>>> x = 1

>>> while x < 5:

print (x)

​ x += 1

What we have here is a variable called ‘x’ and it is equal to a value of 1. In the second line we write the “while” loop condition that says that while x is smaller than 5, the program has to print x. The last line instructs the program to print x’s value + 1 on every loop. If we don’t specify that we’ll end up with an infinite loop that will never stop (more on that later). So, on the first loop, the code sees 1 is smaller than 5, adds 1, and in the next loop x will be equal to 2, which is still smaller than 5, so we’ll have a third loop, and so on. When x reaches a value of 4, the loop will stop running because on the next round x will be equal to 5, which is no longer smaller than 5.

Now, let’s see another example where we tell the program to exit the loop when x reaches a value of 3:

>>> x = 1

>>> while x < 5:

print (x)

if x == 3:

​ break

​ x += 1

The only difference between this example and the previous one is the “if” condition we added. It tells the program to first check if x is equal to 3 before adding +1 to its value. When that condition turns true and x = 3, then the loop will stop running because of the “break” statement.

Now that you understand how ‘while’ loops work, let’s talk about those pesky infinite loops.

Sometimes we want infinite loops because of some code that needs to run again and again for as long as the program is active.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.