/ Control Flow
Documentation

Getting Started

Language Guide

SDL2 and Graphics

CLI Reference

Control Flow

Vexel provides standard conditional branching, range-based and collection-based loops, and loop control statements. All blocks are delimited by indentation.

if / elif / else

Conditional execution uses if, optionally followed by one or more elif branches, and an optional else. The condition must be a bool expression.

fn grade(score: int) -> str:
    if score >= 90:
        return "A"
    elif score >= 80:
        return "B"
    elif score >= 70:
        return "C"
    else:
        return "F"

Conditions can use any combination of comparison and logical operators. Parentheses are optional but allowed.

if x > 0 and x < 100:
    print("in range")

if not active or health == 0:
    print("game over")

for — Range Loop

Iterate over a range of integers using the start..end syntax. The range is exclusive of the end value.

# Prints 0, 1, 2, 3, 4
for i in 0..5:
    print(i)

# Range from variable
let n: int = 10
for i in 0..n:
    print(i * i)

for — Collection Loop

Iterate over every element of an array directly. The loop variable takes on the type of the array element.

let names: str[] = ["alice", "bob", "carol"]
for name in names:
    print(name)

for — Enumerate

When you need both the index and the value, use the two-variable form:

let scores: int[] = [85, 92, 78]
for i, s in scores:
    print(i)   # 0, 1, 2
    print(s)   # 85, 92, 78

while

The while loop executes its body repeatedly as long as the condition is true. The condition is checked before each iteration.

let x: int = 1
while x < 1000:
    x = x * 2
print(x)   # 1024

An infinite loop uses a condition that is always true:

while true:
    let input: int = read_int()
    if input == 0:
        break

break and continue

break exits the innermost loop immediately. continue skips the rest of the current iteration and jumps to the next one.

# Print only even numbers, stop at 10
for i in 0..20:
    if i == 10:
        break
    if i % 2 != 0:
        continue
    print(i)
← Functions Structs →