Skip to content

Control Flow

Control Flow

if / else

Conditional branching uses if (condition): with mandatory parentheses around the condition:

if_else.tg
let a = 40
let b = 2
if (a + b == 42):
print("The answer!")
else:
print("Not the answer")

Nested if/else

nested_if.tg
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:

compound.tg
func check(x: i32, y: i32) -> i32:
if ((x > 0) and (y > 0)):
return 1
if ((x == 0) or (y == 0)):
return 0
return -1

Comparison Operators

OperatorMeaning
==Equal to
!=Not equal to
<Less than
>Greater than
<=Less than or equal to
>=Greater than or equal to
andLogical AND
orLogical OR

while Loop

while_loop.tg
func main() -> i32:
let i = 0
while (i < 5):
print(i)
i = i + 1
return 0
Output:
0
1
2
3
4

Common Pattern: Counter Loop

counter.tg
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 0

Common Pattern: Search Loop

search.tg
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 -1

for..in Range Loop

Iterate over a numeric range using for i in start..end: syntax:

for_range.tg
for i in 0..10:
print(i)

This prints numbers 0 through 9 (the end is exclusive).

match Expression

Pattern matching on enum variants:

match.tg
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:

loop.tg
func find_answer() -> i32:
let n = 0
loop:
n = n + 1
if (n == 42):
return n

Syntax Comparison

thagore.tg
if (x > 0):
print(x)
while (i < 10):
i = i + 1
for j in 0..5:
print(j)

Arithmetic Operators

OperatorMeaningExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Moduloa % b (if supported)