typestar

break and continue in Mojo

Two ways to change a loop's mind partway through.

def main():
    var total = 0
    for n in range(1, 100):
        if n % 7 != 0:
            continue
        if total + n > 200:
            break
        total += n
    print("multiples of 7 summed up to", total)

How it works

  1. continue skips every number that is not a multiple of seven.
  2. break leaves the loop once the total would pass the cap.
  3. total keeps whatever the loop managed to accumulate.

Keywords and builtins used here

The run, in numbers

Lines
9
Characters to type
154
Tokens
44
Three-star pace
90 tpm

At the three-star pace of 90 tokens a minute, this run takes about 29 seconds.

Type this snippet

Step 4 of 5 in Flow control, step 10 of 26 in Language basics.

← Previous Next →