Skip to content

Commit 6238558

Browse files
Update loops
Add better explanation of how loops works
1 parent eda5000 commit 6238558

File tree

1 file changed

+15
-4
lines changed
  • content/micropython/03.micropython/01.basics/02. loops

1 file changed

+15
-4
lines changed

content/micropython/03.micropython/01.basics/02. loops/loops.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,23 @@ Loops are fundamental constructs in programming that allow you to execute a bloc
1212

1313
## Loop Structures in MicroPython
1414

15-
MicroPython supports two primary loop structures:
15+
MicroPython supports two primary loop structures, each with a specific purpose:
1616

17-
- **`for` loops**: Iterate over a sequence (like a list, tuple, or string).
18-
- **`while` loops**: Continue executing as long as a condition is true.
17+
- **`for` loops**: These loops iterate over a predefined sequence, such as a list, tuple, or string. The loop automatically retrieves each item in the sequence, one at a time, and performs actions until every item has been handled.
1918

20-
Let's delve into each of these with examples.
19+
- **`while` loops**: These loops continue executing as long as a specified condition is true. Unlike `for` loops, which depend on a sequence, `while` loops rely on a conditional expression that determines when the loop should stop.
20+
21+
To better understand these loops, let’s imagine them as tasks at the supermarket:
22+
23+
- **`for` loops**: Imagine walking down a supermarket aisle with a shopping list that specifies exactly how many items to pick up, one by one, in order. Once you’ve gathered all the items on your list, your task is complete. This is like a `for` loop iterating over a sequence, handling each specified item one at a time.
24+
25+
TODO:!(FOR ILUSTRATION)[]
26+
27+
- **`while` loops**: Imagine going to the supermarket to buy a certain product that’s on sale, as long as it stays in stock. You keep coming back, day after day, until the sale ends or the stock runs out. In a `while` loop, you keep “coming back” as long as a condition (like the sale continuing) remains true.
28+
29+
TODO:!(WHILE ILUSTRATION)[]
30+
31+
Let's jump into each of these with examples.
2132

2233

2334

0 commit comments

Comments
 (0)