Skip to the content.

Loops_ipynb_2_

#numbers ← [1, 2, 3, 4, 5]
#FOR EACH num IN numbers:
 
#    IF num EQUALS 3:
#        CONTINUE
#    IF num EQUALS 5:
#       BREAK 
#    DISPLAY num
#END FOR


# Example 8: For loop with continue and break
numbers = [1, 2, 3, 4, 5]
for num in numbers:
    if num == 3:
        continue  # Skip the number 3
    if num == 5:
        break  # Exit the loop when 5 is encountered
    print(num)
1
2
4
# APCSP Pseudo-Code: Handling Errors in a For Loop

#numbers ← [1, 2, "three", 4, 0, "five"]
#FOR EACH item IN numbers:
#    TRY:
#        DISPLAY 10 / item
#    CATCH ZeroDivisionError:
#        DISPLAY "Division by zero"
#    CATCH TypeError:
#        DISPLAY "Type error"
#    END TRY
#END FOR


numbers = [1, 2, "three", 4, 0, "five"]

for item in numbers:
    try:
        print(10 / item)
    except ZeroDivisionError: #Type of error: Dividing by Zero
        print("Division by zero")
    except TypeError: #Type of error: Dividing by something that isn't a number
        print("Type error")

10.0
5.0
Type error
2.5
Division by zero
Type error