Skip to content

Commit 6e81b61

Browse files
committed
control structures
1 parent a11819c commit 6e81b61

File tree

1 file changed

+127
-14
lines changed

1 file changed

+127
-14
lines changed

content/micropython/01.basics/08.reference/reference.md

Lines changed: 127 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,13 @@ For example:
109109
- [loop()](#loop)
110110
- [setup()](#setup)
111111
- [Control Structure](#control-structure)
112-
- [break](#break)
113-
- [continue](#continue)
114-
- [do...while](#dowhile)
112+
- [if](#if)
115113
- [else](#else)
116114
- [for](#for)
117-
- [goto](#goto)
118-
- [if](#if)
119-
- [return](#return)
120-
- [switch...case](#switchcase)
121115
- [while](#while)
116+
- [break](#break)
117+
- [continue](#continue)
118+
- [return](#return)
122119
- [Operators](#operators)
123120
- [Arithmetic Operators](#arithmetic-operators)
124121
- [Comparison Operators](#comparison-operators)
@@ -1486,17 +1483,133 @@ while True:
14861483

14871484
## Control Structure
14881485

1489-
### break
1490-
### continue
1491-
### do...while
1486+
Control structures are used to control the flow of a program, i.e. what code will and won't execute. When a condition is met, the specified code block executes.
1487+
1488+
### if
1489+
1490+
The `if` statement checks *if* a condition is met, and executes the following block of code.
1491+
1492+
**Example:**
1493+
1494+
```python
1495+
x = 10
1496+
if x > 5:
1497+
print("x is greater than 5")
1498+
```
1499+
14921500
### else
1501+
1502+
The `else` statement can be used after e.g. an `if` statement. If the previous condition is not met, then it will the block of code following the `else` statement
1503+
1504+
**Example:**
1505+
1506+
```python
1507+
x = 5
1508+
y = 10
1509+
1510+
if x > y:
1511+
print("x is greater")
1512+
else:
1513+
print("y is greater")
1514+
```
1515+
14931516
### for
1494-
### goto
1495-
### if
1496-
### return
1497-
### switch...case
1517+
1518+
The `for` loop is used to iterate through a sequence, e.g. a range of numbers, a list, or a string.
1519+
1520+
**Example 1: Number Range**
1521+
1522+
```python
1523+
for number in range(5):
1524+
print(number)
1525+
```
1526+
1527+
**Example 2: List**
1528+
1529+
```python
1530+
my_list = [10, 20, 30, 40, 50]
1531+
for item in my_list:
1532+
print(item)
1533+
```
1534+
1535+
**Example 3: String**
1536+
1537+
```python
1538+
my_string = "Hello World!"
1539+
for char in my_string:
1540+
print(char)
1541+
```
1542+
14981543
### while
14991544

1545+
The while loop is used to repeatedly execute a block of code as long as the specified condition is `True`.
1546+
1547+
**Example 1:**
1548+
1549+
```python
1550+
i = 0
1551+
while i < 5:
1552+
print(i)
1553+
i += 1
1554+
```
1555+
1556+
**Example 2: Infinity Loop**
1557+
1558+
The while loop is critical to MicroPython as it is needed to create the "infinity" loop, i.e. a loop that runs whenever the board is powered.
1559+
1560+
```python
1561+
while True:
1562+
# your program
1563+
```
1564+
1565+
1566+
1567+
### break
1568+
1569+
The `break` statement is used to break out of loops before it ends.
1570+
1571+
**Example:**
1572+
1573+
```python
1574+
for i in range(5):
1575+
if i == 3:
1576+
break
1577+
print(i)
1578+
1579+
```
1580+
1581+
### continue
1582+
1583+
The `continue` statement is used to skip to the end of the code block, effectively moving on to the next iteration. This is useful to e.g. filter out specific data points.
1584+
1585+
**Example:**
1586+
1587+
This example iterates through a list of numbers. If the number is less than `0` (negative), it will skip it.
1588+
1589+
```python
1590+
my_list = [0, -5, -3, 8, 9 , 11]
1591+
for num in my_list:
1592+
if num < 0:
1593+
continue
1594+
print(num)
1595+
```
1596+
1597+
### return
1598+
1599+
Terminates a function and `return` a value from a function to the calling function.
1600+
1601+
**Example:**
1602+
1603+
In this example, we call a function named `add_numbers()`, which takes two parameters. It then returns the processed value, using the `return` statement.
1604+
1605+
```python
1606+
def add_numbers(a, b):
1607+
result = a + b
1608+
return result
1609+
1610+
print(add_numbers(5,5))
1611+
```
1612+
15001613
## Operators
15011614

15021615
### Arithmetic Operators

0 commit comments

Comments
 (0)