2. while True loops

while-loops run a set of statements as long as a test condition is true.

2.1. While true

../_images/while_True.png
while True: loops run forever.
Instead of using a condition that returns True or False, True is used in place of the condition to make the condition always True. As a result, the while-loop runs forever.
The statements within the loop are indented with the tab key (which is equivalent to 4 spaces) to make those lines part of the while-loop.
The while True loop below scrolls the text I never stop across the LED display, over and over again.
from microbit import *

while True:
    display.scroll('I never stop')

When testing code on the microbit, it can be useful to do it within a while True loop so the results can be seen over and over again.


Questions

Find the error in the first line of these while-loops.

  1. while true:

  2. While True:

  3. while True

while true: error

# must have uppper case T
while True:

While True: error

# must have lower case w
while True:

while True error

# must have colon at end
while True: