Core syntax Control flow

Statements and Control Flow

Klyn uses indentation-defined blocks and a compact statement vocabulary. This page covers the control-flow statements you will use constantly in real code.

Assignment
count = 10
name as String = "Ada"
count += 1
count--

Assignment updates an existing variable while preserving its established type.

If, Elif, Else
score = 72
if score >= 90:
    print("excellent")
elif score >= 50:
    print("pass")
else:
    print("retry")
Match
value = 2
match value:
    case 0:
        label = "zero"
    case 1 | 2:
        label = "small"
    case 3 to 10:
        label = "range"
    default:
        label = "other"

match compares one subject against ordered cases. A case can use an exact value, several alternatives separated by |, or an inclusive interval with to. default handles the fallback branch.

text = "dominique@example.com"
match text:
    case /^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]{2,}$/:
        kind = "email"
    case /^[0-9]{2}-[0-9]{2}-[0-9]{4}$/:
        kind = "date"
    default:
        kind = "unknown"

Regex cases use the same literal syntax as regular expressions and test the subject with pattern matching.

command = "ls"
action = match command:
    case "ls" | "dir":
        "listing"
    case "ps":
        "processes"
    default:
        "unknown"

A match expression returns the expression produced by the first matching branch.

While Loops
i = 0
while i < 3:
    print(i)
    i += 1

Use while when the continuation condition depends on values updated in the body.

Range Loops
for i = 0 to 5:
    print(i)

Both bounds are inclusive. The example prints 0 through 5. When iterating over indices, use an explicit upper bound such as values.size - 1.

Collection Iteration
for value in [10, 20, 30]:
    print(value)

config = {"theme": "dark", "fullscreen": true}

for key in config:
    print(key)

for pair in config.items():
    print(pair)

Iterating directly over a map yields keys. Use keys(), values(), or items() when you want a specific view.

Break and Continue
while true:
    text = input("> ")
    if text == "":
        continue
    if text == "quit":
        break
    print(text)
Assert and Pass
count = 1
featurePending = true

assert count > 0

if featurePending:
    pass

assert is appropriate for invariants and tests. pass is an explicit placeholder that leaves a block intentionally empty.