Repository navigation
Loops (For, While)
Dflat provides a clean, modern syntax for iteration, supporting conditional while loops, range-based for loops, and collection iterators.
Dflat supports both condition-first and body-first (do-while) loop structures.
-
Standard While: Evaluates the condition before executing the block.
while i < n { Print(i) } -
Body-First While: Executes the block first, then evaluates the condition to determine if it should repeat.
{ a-- } while a > 0
In both cases, the condition does not require parentheses and must be castable to a boolean.
The for loop in Dflat relies on ranges and iterators rather than traditional C-style iteration blocks. The loop variable always requires a type (explicitly stated or inferred via var). An untyped loop variable (e.g., for x in ...) results in a compile error.
-
Exclusive Ranges (
..): Iterates up to, but not including, the final value.for u8 i = 0..10 { } -
Inclusive Ranges (
..=): Iterates up to and including the final value. Step increments can be appended using thestepkeyword.for u16 i = 0..=100 step i += 2 { } -
Decreasing Ranges: Counts down automatically when the start value is greater than the end value.
for u8 i = 10..=1 { } -
Collection Iteration (
in): Directly iterates over strings, lists, maps (with support for keys, values, or tuple destructuring), and ranges as virtual collections.for char c in "text" { } for u64 k in list.keys { } for string message in messages { } for (string key, i32 value) in map { } for var (key, value) in map { }
Dflat provides precise control over loop execution using standard keywords and label targeting.
- Continue: Skips the rest of the current iteration and proceeds to the next. Takes no labels.
-
Break: Exits the innermost loop immediately. It can optionally target a specific label to break out of nested loops (e.g.,
break #Found#). -
Jump: Explicitly jumps to a defined label (e.g.,
jump #retry#). Jumps are strictly limited to the same block or an outer block; jumping into an inner block is prohibited. -
Validation: Using
breakorcontinueoutside of a loop results in a compile error.
for i32 i = 0..max {
for i32 x = 0..w {
for i32 y = 0..h {
if y > x then break
if i == y * x then jump #outside#
}
}
#outside#
}
Labels are defined by wrapping a name in hashtags (e.g., #name#), must start and end with #, have no spaces, and contain at least one letter (allowed characters include letters, digits, and _ - + ! ? $ %).