typestar

if, elif, and else in Mojo

Branching that reads exactly like Python.

def grade(score: Int) -> String:
    if score >= 90:
        return "A"
    elif score >= 80:
        return "B"
    elif score >= 70:
        return "C"
    else:
        return "F"

def main():
    for score in [95, 83, 71, 40]:
        print(score, grade(score))

How it works

  1. grade returns as soon as one threshold matches.
  2. elif chains the remaining cases in descending order.
  3. else catches everything the chain missed.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
205
Tokens
70
Three-star pace
95 tpm

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

Type this snippet

Step 1 of 5 in Flow control, step 7 of 26 in Language basics.

← Previous Next →