What Are Break and Continue Statements In Mojo?

We have learned Mojo Loops previously, but we can make loops more flexible using the break and continue statements. These control flow tools help developers manage how loops behave when certain conditions are met.

Introduction to break and continue in Mojo

First we learn what is break and continue:

  • break: This keyword exits the current loop immediately, skipping all remaining iterations.
  • continue: Skips only the current iteration and moves to further operations.

Both statements are used inside for and while loops to modify the loop flow based on the condition.

Why Use break and continue?

break is used to stop a loop when it reaches to the goal and continue ignoring unwanted conditions and keep checking others. This method is most important in real-life industry projects because we don’t write extra code with these keywords.

Syntax of break and continue in Mojo

# break
for i in range(0, 10):
if i == 5:
break
print(i)

# continue
for i in range(0, 10):
if i == 5:
continue
print(i)

We write two conditions in the code, the first loop will print from 0 to 4 and then break that loop automatically. Because we set the break in the condition.

The second loop will print 0 to 4, then skip 5 and then print 6 to 9. Because we have applied the condition with the continue statement.

Examples of Break and Continue

Example 1: Find the First Even Number Greater Than 20

fn main():
var num: Int = 21

while num < 50:
if num % 2 == 0:
print("First even number after 20 is:", num)
break
num += 1
  • This loop will starts with 21 then finds first even number (22). after break is used to exit the loop immediately.

Example 2: Skip All Numbers Which Are Divisible By 3.

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

Output:

1
2
4
5
7
8
10

This loop will skip 3, 6, and 9 because we applied this i % 3 == 0 condition. These three numbers are divisible by 3, so they will be skipped and continue to the next number.

Real-Life Example (Student Project Style)

Project: Library Book Scanner

We will create a simple book scanning system that will stop scanning once it finds a damaged book, and skips empty slots.

fn main():
let books = ["Math", "", "Science", "Damaged", "English"]

for book in books:
if book == "":
continue # skip empty slot
if book == "Damaged":
print("Damaged book found. Stop scanning.")
break
print("Book scanned:", book)

Output:

Book scanned: Math
Book scanned: Science
Damaged book found. Stop scanning.

Try to understand this code by yourself. If you face any problem, so send us on our official email, and we will respond to you within the next 24 hours.

Exercise for Students

Write a Mojo program that checks a list of temperatures. The loop should:

  • Skip any temperature below 0°C using the continue statement.
  • Stop checking when it finds a temperature above 45°C using the break statement.
  • Print only valid temperatures between 0°C and 45°C

Input:

temps = [12, -5, 20, 33, 48, 27]

Your expected output:

Valid temperature: 12°C
Valid temperature: 20°C
Valid temperature: 33°C
High temperature alert: 48°C

Code Logic:

temps = [12, -5, 20, 33, 48, 27]

for temp in temps:
if temp < 0:
# Skip this temperature as it's below 0°C
continue

if temp > 45:
# Alert and stop checking further
print("High temperature alert:", temp, "°C")
break

# Only prints valid temperatures
print("Valid temperature:", temp, "°C")

Try this code by yourself and then use this as your reference.

Also Learn this Topics:

Leave a Comment