Control Flow
Control Flow
if / else
Conditional branching uses if (condition): with mandatory parentheses around the condition:
let a = 40let b = 2if (a + b == 42): print("The answer!")else: print("Not the answer")Nested if/else
func classify(n: i32) -> String: if (n > 0): return "positive" else: if (n < 0): return "negative" else: return "zero"Chained Conditions
Use and and or for compound conditions:
func check(x: i32, y: i32) -> i32: if ((x > 0) and (y > 0)): return 1 if ((x == 0) or (y == 0)): return 0 return -1Comparison Operators
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal to |
< | Less than |
> | Greater than |
<= | Less than or equal to |
>= | Greater than or equal to |
and | Logical AND |
or | Logical OR |
while Loop
func main() -> i32: let i = 0 while (i < 5): print(i) i = i + 1 return 0Output:01234Common Pattern: Counter Loop
func sum_to(n: i32) -> i32: let i = 0 let total = 0 while (i <= n): total = total + i i = i + 1 return total
func main() -> i32: let result = sum_to(100) print(result) # Output: 5050 return 0Common Pattern: Search Loop
func find_index(arr: [i32; 4], target: i32) -> i32: let i = 0 while (i < 4): if (arr[i] == target): return i i = i + 1 return -1for..in Range Loop
Iterate over a numeric range using for i in start..end: syntax:
for i in 0..10: print(i)This prints numbers 0 through 9 (the end is exclusive).
match Expression
Pattern matching on enum variants:
enum Color: Red Green Blue
func describe(c: Color) -> String: match c: Red: return "warm" Green: return "natural" Blue: return "cool"match requires at least one enum declaration and is validated by the compiler’s feature-edge checks.
loop Keyword
An explicit infinite loop construct:
func find_answer() -> i32: let n = 0 loop: n = n + 1 if (n == 42): return nSyntax Comparison
if (x > 0): print(x)
while (i < 10): i = i + 1
for j in 0..5: print(j)if x > 0: print(x)
while i < 10: i += 1
for j in range(5): print(j)if (x > 0) { printf("%d", x); }
while (i < 10) { i++; }
for (int j = 0; j < 5; j++) { printf("%d", j);}Arithmetic Operators
| Operator | Meaning | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulo | a % b (if supported) |