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
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
+
1492
1500
### 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
+
1493
1516
### 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 inrange(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
+
1498
1543
### while
1499
1544
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
+
whileTrue:
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 inrange(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.
0 commit comments