🔀 Control Flow
Conditionals, loops, and flow control in RADS
If-Else Statements
RADS uses if, elif, and else for conditional execution.
Basic If Statement
blast main() {
turbo i32 age = 25;
if (age >= 18) {
echo("Adult");
}
}
If-Else
blast main() {
turbo i32 score = 85;
if (score >= 90) {
echo("Grade: A");
} else {
echo("Grade: B or lower");
}
}
If-Elif-Else Chain
blast main() {
turbo i32 speed = 9500;
if (speed > 9000) {
echo("It's over 9000!");
} elif (speed > 5000) {
echo("Pretty fast!");
} elif (speed > 1000) {
echo("Moderate speed");
} else {
echo("Needs more turbo!");
}
}
Comparison Operators
| Operator | Description | Example |
|---|---|---|
== |
Equal to | if (x == 5) |
!= |
Not equal to | if (x != 0) |
> |
Greater than | if (x > 10) |
< |
Less than | if (x < 100) |
>= |
Greater than or equal | if (x >= 18) |
<= |
Less than or equal | if (x <= 99) |
Logical Operators
| Operator | Description | Example |
|---|---|---|
&& |
Logical AND | if (x > 0 && x < 100) |
|| |
Logical OR | if (x == 0 || x == 1) |
! |
Logical NOT | if (!is_valid) |
blast main() {
turbo i32 age = 25;
turbo bool has_license = true;
// AND operator
if (age >= 18 && has_license) {
echo("Can drive");
}
// OR operator
if (age < 13 || age > 65) {
echo("Discounted ticket");
}
// NOT operator
if (!has_license) {
echo("Need to get a license");
}
}
Loop (While Loop)
The loop keyword creates a while loop that continues as long as the condition is true.
Basic Loop
blast main() {
turbo i32 i = 0;
loop (i < 5) {
echo("Count: " + i);
i = i + 1;
}
}
Infinite Loop with Break
blast main() {
turbo i32 count = 0;
loop (true) {
echo("Iteration: " + count);
count = count + 1;
if (count >= 10) {
break;
}
}
}
Loop with Continue
blast main() {
turbo i32 i = 0;
loop (i < 10) {
i = i + 1;
// Skip even numbers
if (i % 2 == 0) {
continue;
}
echo("Odd number: " + i);
}
}
Cruise (For Loop)
The cruise keyword creates a for loop that iterates over ranges or collections.
Cruise Over Range
blast main() {
// Cruise from 0 to 4 (exclusive end)
cruise (i in 0..5) {
echo("i = " + i);
}
// Cruise from 10 to 20
cruise (n in 10..21) {
echo("n = " + n);
}
}
Cruise Over Array
blast main() {
array<str> names = ["Alice", "Bob", "Charlie"];
cruise (name in names) {
echo("Hello, " + name + "!");
}
// Cruise over numbers
array<i32> scores = [95, 87, 92, 78];
cruise (score in scores) {
echo("Score: " + score);
}
}
Cruise with Break and Continue
blast main() {
array<i32> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
cruise (num in numbers) {
// Stop at 6
if (num > 6) {
break;
}
// Skip 3
if (num == 3) {
continue;
}
echo("Number: " + num);
}
}
Switch Statements
Switch statements provide multi-way branching based on a value.
Basic Switch
blast main() {
turbo i32 day = 3;
switch (day) {
case 1:
echo("Monday");
break;
case 2:
echo("Tuesday");
break;
case 3:
echo("Wednesday");
break;
case 4:
echo("Thursday");
break;
case 5:
echo("Friday");
break;
default:
echo("Weekend");
}
}
Switch with Multiple Cases
blast main() {
turbo char grade = 'B';
switch (grade) {
case 'A':
echo("Excellent!");
break;
case 'B':
echo("Good job!");
break;
case 'C':
echo("Satisfactory");
break;
case 'D':
echo("Needs improvement");
break;
case 'F':
echo("Failed");
break;
default:
echo("Invalid grade");
}
}
Control Flow Keywords
| Keyword | Description | Usage |
|---|---|---|
if |
Conditional execution | if (condition) { ... } |
elif |
Else-if condition | elif (condition) { ... } |
else |
Default branch | else { ... } |
loop |
While loop | loop (condition) { ... } |
cruise |
For loop | cruise (item in collection) { ... } |
switch |
Multi-way branch | switch (value) { ... } |
case |
Switch case | case value: ... |
default |
Default switch case | default: ... |
break |
Exit loop/switch | break; |
continue |
Skip to next iteration | continue; |
return |
Return from function | return value; |
💡 Best Practices
- Always use
breakin switch statements to prevent fall-through - Prefer
cruiseoverloopwhen iterating over collections - Use
elifchains instead of nested if statements for readability - Avoid infinite loops without a clear exit condition
- Use meaningful variable names in cruise loops (e.g.,
cruise (user in users))