Control Flow¶
FastC provides familiar control flow constructs for conditionals and loops.
If Statements¶
Basic If¶
If-Else¶
If-Else If-Else¶
Example¶
fn classify(n: i32) -> i32 {
if n < 0 {
return -1;
} else if n == 0 {
return 0;
} else {
return 1;
}
}
If-Let (Optional Unwrapping)¶
Unwrap optional values safely:
fn process(maybe_value: opt(i32)) -> i32 {
if let value = maybe_value {
// value is i32 here, not opt(i32)
return value * 2;
} else {
return 0;
}
}
See Optionals for more details.
While Loops¶
Example¶
For Loops¶
FastC uses C-style for loops:
Example¶
fn sum_to_n(n: i32) -> i32 {
let total: i32 = 0;
for let i: i32 = 1; i <= n; i = i + 1 {
total = total + i;
}
return total;
}
Iterating Over a Range¶
Countdown¶
Break and Continue¶
Break¶
Exit a loop early:
fn find_first_negative(data: slice(i32)) -> i32 {
let result: i32 = -1;
for let i: i32 = 0; i < len(data); i = i + 1 {
if at(data, i) < 0 {
result = at(data, i);
break;
}
}
return result;
}
Continue¶
Skip to the next iteration:
fn sum_positive(data: slice(i32)) -> i32 {
let total: i32 = 0;
for let i: i32 = 0; i < len(data); i = i + 1 {
if at(data, i) < 0 {
continue;
}
total = total + at(data, i);
}
return total;
}
Switch Statements¶
Match a value against multiple cases:
Example¶
fn day_name(day: i32) -> i32 {
switch day {
case 1:
return 1; // Monday
case 2:
return 2; // Tuesday
case 3:
return 3; // Wednesday
default:
return 0; // Unknown
}
}
With Enums¶
enum Status {
Pending,
Running,
Complete,
Failed,
}
fn handle_status(status: Status) -> i32 {
switch status {
case Status::Pending:
return 0;
case Status::Running:
return 1;
case Status::Complete:
return 2;
case Status::Failed:
return -1;
default:
return -2;
}
}
Return Statements¶
Exit a function and optionally return a value:
fn early_return(x: i32) -> i32 {
if x < 0 {
return -1; // Early return
}
// Normal processing
return x * 2;
}
Void Functions¶
Use return; without a value:
Conditions Must Be Boolean¶
Unlike C, conditions must be explicitly boolean:
let x: i32 = 5;
// Error: i32 is not bool
if x {
// ...
}
// Correct: explicit comparison
if x != 0 {
// ...
}
Short-Circuit Evaluation¶
Logical operators && and || short-circuit:
// b() is only called if a() returns true
if a() && b() {
// ...
}
// b() is only called if a() returns false
if a() || b() {
// ...
}
This is important for avoiding null checks: