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
gradereturns as soon as one threshold matches.elifchains the remaining cases in descending order.elsecatches everything the chain missed.
Keywords and builtins used here
IntStringdefelifelseforgradeifmainprintreturn
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.
Step 1 of 5 in Flow control, step 7 of 26 in Language basics.