You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/micropython/03.micropython/01.basics/02. loops/loops.md
+100-16
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,55 @@
1
1
---
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.'
6
4
author: 'Pedro Lima'
7
-
hero_image: "./hero-banner.png"
8
-
5
+
tags: [MicroPython, Loops]
9
6
---
10
7
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:
-[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]().***
12
53
13
54
## Loop Structures in MicroPython
14
55
@@ -22,17 +63,13 @@ To better understand these loops, let’s imagine them as tasks at the supermark
22
63
23
64
-**`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
65
25
-
TODO:!(FOR ILLUSTRATION)[]
66
+
![How for loops work.]()
26
67
27
68
-**`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
69
29
-
TODO:!(WHILE ILLUSTRATION)[]
30
-
31
-
Let's jump into each of these with examples.
32
-
33
-
70
+
![How while loops work.]()
34
71
35
-
## Using a `for` Loop
72
+
## For Loops
36
73
37
74
The `for` loop is used for iterating over a sequence. It automatically retrieves each item in the sequence one after another.
38
75
@@ -61,9 +98,9 @@ for letter in "Arduino":
61
98
time.sleep(3)
62
99
```
63
100
64
-
**Explanation:**
101
+
Let's take a look at what's included in this code example:
65
102
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.
67
104
-**Initialize `cycle` Variable**: We start a `cycle` counter at 1.
68
105
-**`for letter in "Arduino"`**: The loop iterates over each character in the string `"Arduino"`, assigning each character to the variable `letter`.
69
106
-**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):
116
153
-**Increment Counters**: Increases `index` and `cycle` by 1.
117
154
-**`time.sleep(3)`**: Pauses the program for 3 seconds before the next iteration.
118
155
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
119
161
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:
120
163
164
+
```python
165
+
for i inrange(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
+
```
121
185
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.
0 commit comments