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
continueskips every number that is not a multiple of seven.breakleaves the loop once the total would pass the cap.totalkeeps whatever the loop managed to accumulate.
Keywords and builtins used here
breakcontinuedefforifmainprintrangevar
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.
Step 4 of 5 in Flow control, step 10 of 26 in Language basics.