Control Flow
Control flow statements let your program make decisions and repeat actions. Stellartrails supports if
/else
, for
, and while
constructs, similar to many modern languages. You can also use break
and continue
in loops.
If/Else
Use if
and else
to execute code conditionally:
let x = 10
if (x > 5) {
print("x is greater than 5")
} else {
print("x is not greater than 5")
}
For Loops
Use for
to iterate over arrays and collections:
let arr = [1, 2, 3]
for item in arr {
print(item)
}
While Loops
Use while
to repeat a block as long as a condition is true:
let n = 3
while (n > 0) {
print(n)
n = n - 1
}
Summary
This tutorial covered the control flow constructs in Stellartrails:
- If/Else: Conditional execution of code blocks.
- For Loops: Iteration over collections.
- While Loops: Repeating actions based on conditions.
Control flow is essential for creating dynamic and responsive programs. Experiment with these constructs to get a good grasp of their usage.