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
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.
Ada | Ajax |
Assembly Language Programming | Borland Delphi |
C & C++ | C# |
CSS | Compiler Design |
Compilers | DHTML |
Debugging | Delphi |
Fortran | Java |
Lisp | Perl |
Prolog | Python |
RPG | Ruby |
Swift | Visual Basic |
XHTML | XML |
XSL |
Hello! Python by Anthony Briggs(9682)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9594)
The Mikado Method by Ola Ellnestam Daniel Brolund(9571)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8102)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7618)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7615)
Grails in Action by Glen Smith Peter Ledbrook(7526)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7339)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6237)
Windows APT Warfare by Sheng-Hao Ma(5522)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(5258)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(5104)
Kotlin in Action by Dmitry Jemerov(4887)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4184)
Functional Programming in JavaScript by Mantyla Dan(3898)
Solidity Programming Essentials by Ritesh Modi(3281)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3033)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3011)
Mastering Python for Finance by Weiming James Ma;(2995)
