Control Flow

Astra provides clean, readable control flow with if/else, pattern matching with check, and structured error handling with when/then. Blocks end with ; instead of braces.

if Statement

Execute code only when a condition is true. The block ends with ;.

if.astra
score = 85 if score >= 60 write "You passed!" ;
💡 Note: No parentheses needed around conditions. Indentation is not significant — blocks always end with ;

if / else

Use else to handle the false case:

ifelse.astra
age = 17 if age >= 18 write "Adult" else write "Minor" ;

else if

Chain multiple conditions with else if:

elseif.astra
score = 85 if score >= 90 write "A Grade" else if score >= 80 write "B Grade" else if score >= 70 write "C Grade" else if score >= 60 write "D Grade" else write "F Grade" ;

Nested Conditions

Conditions can be nested and combined with && and ||:

nested.astra
x = 15 y = 8 \\ && (AND) if x > 10 && y > 5 write "Both conditions met" ; \\ || (OR) if x > 20 || y > 5 write "At least one condition met" ; \\ Nested if if x > 10 if y > 5 write "x > 10 and y > 5" else write "x > 10 but y <= 5" ; ;

check Statement

check is Astra's pattern matching — similar to switch in other languages, but cleaner. Use end: for the default case.

check.astra
day = 3 check day 1: write "Monday" 2: write "Tuesday" 3: write "Wednesday" 4: write "Thursday" 5: write "Friday" 6: write "Saturday" 7: write "Sunday" end: write "Invalid day" ;
💡 Tip: Each case automatically breaks — no break needed!

Range & String Matching

Range Match

Match a range of values using start-end:

score = 75 check score 90-100: write "A — Excellent!" 80-89: write "B — Good" 70-79: write "C — Average" 60-69: write "D — Below Average" end: write "F — Failed" ;

String Match

Match string values directly:

lang = "Astra" check lang "Python": write "Snake language" "Astra": write "Lightning fast!" "C++": write "Close to metal" end: write "Unknown language" ;

Mixed Example

check-full.astra
x = user:Enter a number: check x 1: write "One" 2-5: write "Between 2 and 5" 6-10: write "Between 6 and 10" end: write "Out of range" ;

when / then — Error Handling

when executes a block and catches errors with then. This is Astra's structured error handling — no try/catch needed.

when.astra
when: write undefined_var then UNDEFINED_VAR: write "Variable not found!" then: write "Some other error occurred" ;

Available Error Codes

UNDEFINED_VAR \\ Variable used without being defined UNINITIALIZED_VAR \\ Variable declared but not assigned DIVISION_BY_ZERO \\ Division by zero FUNC_NOT_FOUND \\ Function not found FIELD_NOT_FOUND \\ Chain field not found INVALID_OPERATION \\ Invalid type operation

General catch

Use then: without an error code to catch any error:

when: result = riskyOperation() then: write "Something went wrong!" result = 0 ;
📖 See also: Full error handling docs in Error Handling →