For Loops & Ranges
Learn how to iterate over ranges of numbers seamlessly with for ... to syntax.
1. Inclusive Range Iteration
In NouvaLang, the for loop iterates across an inclusive numerical interval using the to keyword:
// Iterates from 1 to 5 inclusive: (1, 2, 3, 4, 5)
for i in 1 to 5 {
print("Iteration:", i);
}
2. Automatic Direction Detection
The runtime evaluator automatically detects the step direction. If the start number is greater than the end number, it decrements automatically:
// Counts down: 5, 4, 3, 2, 1
for count in 5 to 1 {
print("Countdown:", count);
}
print("Liftoff!");
3. Nested Loops (e.g. Multiplication Matrix)
for row in 1 to 3 {
for col in 1 to 3 {
let result: int = row * col;
print(row, "x", col, "=", result);
}
}