SunFounder ESP32 Starter Kit
(continued from previous page)
break
x -= 1
>>> %Run -c $EDITOR_CONTENT
10
9
8
7
6
While Loop with Else
Like the if loop, the while loop can also have an optional else block.
If the condition in the while loop is evaluated as False, the else part is executed.
x = 10
while x > 0:
print(x)
x -= 1
else:
print("Game Over")
>>> %Run -c $EDITOR_CONTENT
10
9
8
7
6
5
4
3
2
1
Game Over
3.6.7 For Loops
The for loop can traverse any sequence of items, such as a list or a string.
The syntax format of for loop is as follows:
for val in sequence:
Body of for
Here, val is a variable that gets the value of the item in the sequence in each iteration.
The loop continues until we reach the last item in the sequence. Use indentation to separate the body of the for loop
from the rest of the code.
Flowchart of for Loop
3.6. 1.6 (Optional) MicroPython Basic Syntax 289