Conditional Execution
Control flow statements allow you to execute code selectively based on
certain conditions. The most common way to do this is with
if, else if, and else.
if / else if / else / unless
An if statement executes a block of code only if its
condition is true. It can be followed by one or more
else if blocks and an optional else block,
which runs if no preceding conditions were met. The
unless statement is just the opposite of if, I prefer
using unless instead of having lot's of
!(Not) characters everywhere.
data score = 85
if (score >= 90) {
out("Grade: A")
} else if (score >= 80) {
out("Grade: B")
} else {
out("Grade: C or lower")
}
# Output: Grade: B
unless score >= 0 {
# This examples uses the panic keyword you can find it in the error handling section
panic Score cannot be negative!!!"
}
Loops
Loops are used to execute a block of code repeatedly. Ry currently
supports while, foreach and
do-until loops.
while Loops
A while loop continues to execute its code block as long
as its condition remains true. It's important to ensure
the condition will eventually become false to avoid an infinite loop.
data i = 0
while i < 3 {
out("Hello number ${i}")
i = i + 1
}
# Output:
# Hello number 0
# Hello number 1
# Hello number 2
foreach Loops
The foreach loop is ideal when you know exactly how many
times you want to iterate. It consists of an initializer, and an
iterable expression.
foreach data i in 0 to 5 {
out("Iteration: ${i}")
}
# Output: Iteration: 0 ... Iteration: 5
do-until Loops
a do-until loop is like the while loop and
unless statement combined.The unless loop repeats a piece
of code until the condition is false.
data i = 0
do {
out("Hello number ${i}")
i = i + 1
} until i == 10
# Outputs:
# Hello number 0 ... Hello number 9
Congratulations on Mastering the Basics!
You've now covered all the fundamental concepts of the Ry language! You're ready to explore more powerful features in the Advanced section, starting with C++ extensibility.
Next: C++ Modules!