Control Flow
Klar’s control flow is strict by design, aiming to eliminate ambiguity in nested structures.
The Two Golden Rules of Klar Flow:
- No Shorthands: Blocks always use
{ }. Single-lineifstatements without braces are prohibited. - Explicit Termination: Conditional chains must always terminate with the
afterallkeyword.
if / otherwise / afterall
Basic if
Even a simple if requires the afterall; terminator.
if (condition) {
// code to execute
} afterall;
if with Fallback (Else)
In Klar, what other languages call else is handled by the afterall block.
if (condition) {
// code
} afterall {
// fallback logic
}
The otherwise Clause (Else-if)
The otherwise keyword requires a because clause. This forces the developer to document the intent of the alternative branch directly in the syntax.
if (score > 90) {
println("A");
} otherwise (score > 80) because "high performer threshold" {
println("B");
} afterall {
println("F");
}
Syntax Rules
because "..."is mandatory on everyotherwiseblock.afterallis mandatory to close theifchain.- The body of
afterall { ... }is optional, but the keyword (or semicolonafterall;) is not.
Loops
while
Klar supports standard while loops. Unlike the conditional chain, while does not require a special terminator beyond the closing brace.
while (condition) {
// repeat logic
}
Notes on Loops:
- Braces
{}are mandatory even for single statements. whiledoes not useafterall.
Syntax Rules
forloopsforeach/ Iterator-based loopsswitch/ Pattern matching
All of these do not exist at Klar.