typestar

for and range in Mojo

range drives counted loops with one, two, or three arguments.

def main():
    for i in range(4):
        print("count", i)
    for i in range(2, 11, 3):
        print("step", i)
    for i in range(10, 0, -5):
        print("down", i)

How it works

  1. range(4) counts from zero up to but not including four.
  2. range(2, 11, 3) adds a start and a stride.
  3. A negative step counts down instead of up.

Keywords and builtins used here

The run, in numbers

Lines
7
Characters to type
135
Tokens
57
Three-star pace
95 tpm

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

Type this snippet

Step 3 of 5 in Flow control, step 9 of 26 in Language basics.

← Previous Next →