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
range(4)counts from zero up to but not including four.range(2, 11, 3)adds a start and a stride.- A negative step counts down instead of up.
Keywords and builtins used here
defformainprintrange
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.
Step 3 of 5 in Flow control, step 9 of 26 in Language basics.