Skip to content

Commit fe46696

Browse files
committed
review loops pt1
1 parent d35a093 commit fe46696

File tree

1 file changed

+100
-16
lines changed
  • content/micropython/03.micropython/01.basics/02. loops

1 file changed

+100
-16
lines changed

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

+100-16
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,55 @@
11
---
2-
3-
featured: micropython-101
4-
title: '3. Micropython Basics - Loops'
5-
description: 'Learn the basics for loops on MicroPython.'
2+
title: 'Loops'
3+
description: 'Learn how to use different loops with MicroPython.'
64
author: 'Pedro Lima'
7-
hero_image: "./hero-banner.png"
8-
5+
tags: [MicroPython, Loops]
96
---
107

11-
Loops are fundamental constructs in programming that allow you to execute a block of code multiple times. In MicroPython, loops help you perform repetitive tasks efficiently and are an awesome tool to keep in your coder's toolbox. In this article, we will explore the different loop structures available.
8+
Loops are fundamental constructs in all programming languages, that allow you to execute a block of code multiple times. In MicroPython, loops help you perform repetitive tasks efficiently and are an awesome tool to keep in your coder's toolbox.
9+
10+
In this guide, we will explore the different loop structures available.
11+
12+
## Requirements
13+
14+
Before we start, let's check the requirements:
15+
16+
### MicroPython Compatible Arduino Boards
17+
18+
MicroPython is officially supported on several Arduino boards. Here’s a list of the compatible boards:
19+
20+
- [Portenta C33](https://store.arduino.cc/products/portenta-c33)
21+
- [Arduino GIGA R1 WiFi](https://store.arduino.cc/products/arduino-giga-r1-wifi)
22+
- [Portenta H7](https://store.arduino.cc/products/portenta-h7)
23+
- [Portenta H7 Lite](https://store.arduino.cc/products/portenta-h7-lite)
24+
- [Portenta H7 Lite Connected](https://store.arduino.cc/products/portenta-h7-lite-connected)
25+
- [Opta](https://store.arduino.cc/products/opta)
26+
- [Opta Wifi](https://store.arduino.cc/products/opta-wifi)
27+
- [Opta RS485](https://store.arduino.cc/products/opta-rs485)
28+
- [Arduino Nano RP2040 Connect](https://store.arduino.cc/products/arduino-nano-rp2040-connect)
29+
- [Nicla Vision](https://store.arduino.cc/products/nicla-vision)
30+
- [Arduino Nano 33 BLE](https://store.arduino.cc/products/arduino-nano-33-ble)
31+
- [Arduino Nano 33 BLE Rev2](https://store.arduino.cc/products/arduino-nano-33-ble-rev2)
32+
- [Arduino Nano 33 BLE Sense](https://store.arduino.cc/products/arduino-nano-33-ble-sense)
33+
- [Arduino Nano 33 BLE Sense Rev2](https://store.arduino.cc/products/arduino-nano-33-ble-sense-rev2)
34+
- [Arduino Nano ESP32](https://store.arduino.cc/products/arduino-nano-esp32)
35+
36+
### Software Requirements
37+
38+
- [Arduino Lab for Micropython](https://labs.arduino.cc/en/labs/micropython) - Arduino Lab for MicroPython is an editor where we can create and run MicroPython scripts on our Arduino board.
39+
40+
***Note that the editor is also available online, at [Arduino Cloud - Arduino Labs for MicroPython](https://lab-micropython.arduino.cc/)***
41+
42+
## Board and Editor Setup
43+
44+
1. Open the [Arduino Lab for MicroPython]() application.
45+
2. Plug the Arduino board into the computer using a USB cable.
46+
![Connect board to computer.]()
47+
3. Press the connection button on the top left corner of the window.
48+
![Connect the editor to the board.]()
49+
4. The connected Arduino board should appear, and we can click it:
50+
![Select board.]()
51+
52+
***Need help installing MicroPython on your board? Visit the [MicroPython installation guide]().***
1253

1354
## Loop Structures in MicroPython
1455

@@ -22,17 +63,13 @@ To better understand these loops, let’s imagine them as tasks at the supermark
2263

2364
- **`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.
2465

25-
TODO:!(FOR ILLUSTRATION)[]
66+
![How for loops work.]()
2667

2768
- **`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.
2869

29-
TODO:!(WHILE ILLUSTRATION)[]
30-
31-
Let's jump into each of these with examples.
32-
33-
70+
![How while loops work.]()
3471

35-
## Using a `for` Loop
72+
## For Loops
3673

3774
The `for` loop is used for iterating over a sequence. It automatically retrieves each item in the sequence one after another.
3875

@@ -61,9 +98,9 @@ for letter in "Arduino":
6198
time.sleep(3)
6299
```
63100

64-
**Explanation:**
101+
Let's take a look at what's included in this code example:
65102

66-
- **Import `time` Module**: We import the `time` module to use the `sleep()` function for delays.
103+
- `import time` - we import the `time` module to use the `sleep()` function for delays.
67104
- **Initialize `cycle` Variable**: We start a `cycle` counter at 1.
68105
- **`for letter in "Arduino"`**: The loop iterates over each character in the string `"Arduino"`, assigning each character to the variable `letter`.
69106
- **Print Statement**: Outputs the cycle number, the current letter, and mentions that it's printed with a `for` loop.
@@ -116,9 +153,56 @@ while index < len(word):
116153
- **Increment Counters**: Increases `index` and `cycle` by 1.
117154
- **`time.sleep(3)`**: Pauses the program for 3 seconds before the next iteration.
118155

156+
## Control Statements
157+
158+
While inside a loop, we can control how it should behave using control statements: **continue** and **break**
159+
160+
### Continue
119161

162+
The `continue` statement can be used to skip past an iteration. For example, if we for some reason want to skip every fifth iteration, we could use the following code:
120163

164+
```python
165+
for i in range(10)
166+
if i == 5:
167+
continue
168+
print(i)
169+
```
170+
171+
Running this script will result in:
172+
173+
```python
174+
0
175+
1
176+
2
177+
3
178+
4
179+
6 # we skip the 5th iteration
180+
7
181+
8
182+
9
183+
10
184+
```
121185

186+
### Break
187+
188+
The `break` statement can be used to break out of a loop before it finishes all iterations. This can be useful to for example cancel a process if something unexpected happens.
189+
190+
```python
191+
for i in range(10)
192+
if i == 5:
193+
break
194+
print(i)
195+
```
196+
197+
Running this script will result in:
198+
199+
```python
200+
0
201+
1
202+
2
203+
3
204+
4
205+
```
122206

123207
## Conclusion
124208

0 commit comments

Comments
 (0)