Loop Control Statements in MicroPython on Raspberry Pi Pico

Posted

in

by

Loop control statements are used to alter the normal flow of execution within loops. They can be used to skip iterations or terminate loops prematurely based on certain conditions. In MicroPython on Raspberry Pi Pico, there are three loop control statements: break, continue, and pass.

  1. Break statement:
    The break statement is used to terminate a loop prematurely. When a break statement is encountered within a loop, the loop is immediately exited and program execution continues with the statement immediately following the loop. The break statement can be used with both for and while loops.

Here’s an example:

for i in range(1, 11):
    if i == 5:
        break
    print(i)

In this example, the loop will iterate over the values from 1 to 10. However, when i equals 5, the break statement is encountered and the loop is terminated prematurely. As a result, only the values from 1 to 4 will be printed.

  1. Continue statement:
    The continue statement is used to skip the current iteration of a loop and move on to the next iteration. When a continue statement is encountered within a loop, the remaining statements within the loop for that iteration are skipped and the loop moves on to the next iteration. The continue statement can be used with both for and while loops.

Here’s an example:

for i in range(1, 11):
    if i % 2 == 0:
        continue
    print(i)

In this example, the loop will iterate over the values from 1 to 10. However, when i is even, the continue statement is encountered and the remaining statements for that iteration are skipped. As a result, only the odd values from 1 to 10 will be printed.

  1. Pass statement:
    The pass statement is used to do nothing. It can be used as a placeholder when a statement is required syntactically, but no action is needed. The pass statement is typically used when a block of code is not yet implemented but is required for the program to run without error.

Here’s an example:

for i in range(1, 11):
    pass

In this example, the loop will iterate over the values from 1 to 10. However, the pass statement does nothing, so the loop will simply run through all iterations without doing anything. The pass statement can be used with both for and while loops.

Loop control statements are a useful tool in programming, as they allow us to alter the normal flow of execution within loops based on certain conditions. The break statement can be used to terminate a loop prematurely, the continue statement can be used to skip the current iteration and move on to the next iteration, and the pass statement can be used as a placeholder when no action is needed.

Sure, here is an example of how you can use continue, pass, and break statements in a loop:

# Example of using loop control statements in MicroPython

# Loop from 1 to 10
for i in range(1, 11):
    # If i is even, skip to the next iteration
    if i % 2 == 0:
        continue

    # If i is 5, do nothing and continue to the next iteration
    if i == 5:
        pass

    # If i is greater than 8, break out of the loop
    if i > 8:
        break

    # Print the value of i
    print(i)

In this example, the continue statement is used to skip over even numbers in the loop. The pass statement is used to do nothing and simply continue to the next iteration when the value of i is 5. Finally, the break statement is used to exit the loop when the value of i becomes greater than 8.

When you run this code, you will see the following output:

1
3
7

As you can see, the even numbers (2, 4, 6, 8, and 10) are skipped over with the continue statement. The value of 5 does nothing with the pass statement, and the loop stops when the value of i reaches 9 because of the break statement.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *