Programs That Can Loop
Explore how Python loops work by using the while statement to run code repeatedly based on conditions. Learn to control loop execution with counters, avoid common logical errors like infinite loops, and understand program flow beyond sequential execution. This lesson equips you to write efficient repeating tasks in your programs.
We'll cover the following...
We have found a way to break the strictly sequential flow of a program, by pushing one or however many statements within an if block. This means that our lines of code, other than those in if-else blocks, will get executed line by line, in sequence. But some lines of code will only get executed if a condition is True, otherwise, another set of lines will become a part of the execution. Sequential or selective, each line of code only gets executed once, and then our computer fetches the next one, and then the next.
The while loop
Wouldn’t it be great for the programmers if they could make the computer execute a block of code repeatedly while a condition is True?
For instance, if you are required to print 1 three times, what would be your code?
Let’s imagine we are being asked by our boss to print 1 ten times. Of ...