If Statement#
If <condition> then, <statement>.
With else:
If <condition> then, <statement>. Otherwise, <statement>.
With else-if:
If <condition> then, <statement>. But if <condition> then, <statement>. Otherwise, <statement>.
Sentence consumption rule (important):
- Each
then,/but if ... then,/otherwise,branch consumes actions until the sentence ends. - Separate multiple actions in a branch with commas.
- Use a period to end the full
ifsentence. - A period before
but if/otherwiseis treated as part of the same if-chain when the chain continues.
If ready then, print "a", print "b", print "c".
Alternative keywords:
Whencan replaceIfElsecan replaceOtherwise
While Loop#
While <condition>, <statements>.
Single-line example:
While the counter is less than 10, print the counter, increment the counter.
Multi-action loops are comma-separated actions within one sentence:
While x is less than 5, print x, increment x, print "looping".
Loops inside functions work naturally:
To sum of a number called n.
a number called total is 0.
a number called i is 1.
While i is less than or equal to n, total is total add i, i is i add 1.
Return a number, total.
For Each Loop#
Range-based:
For each number from <start> to <end>, <statement>.
Example:
For each number from 1 to 10, print the number.
Inside the loop:
the numberrefers to the current iteration value
List-based:
For each <variable> in <list>, <statement>.
Example:
a list called nums is [1, 2, 3].
For each n in nums, print the n.
Repeat#
Run a body a fixed number of times.
Repeat <count> times, <statements>.
Single-line example:
Repeat 3 times, print "hello".
Multi-action loops are comma-separated actions within one sentence, exactly like While:
Repeat 2 times, print "a", print "b".
This prints a, b, a, b — two actions per iteration, two iterations.
Termination. Repeat closes by the same rules as While and For each: a period ends the body (and closes the construct — rule 1), and a blank line force-closes it (rule 2). The statements after a closing period belong to the surrounding scope, not the loop:
Repeat 2 times, print "r".
Print "after".
→ r r after
Because a period closes the construct, periods stack: write one period per level you want to close, so a Repeat nested in another loop takes two periods to close both (see Closing more than one level):
For each n from 1 to 2,
Repeat 2 times, print "r"..
Print "after".
→ r r r r after
Loop Control#
Break.
Continue.
Program Termination#
Immediately exit the program with an exit code:
Exit <code>.
Examples:
Exit 0. (Success)
Exit 1. (General error)
If arguments's empty then,
Print "Usage: ./program <file>".
Exit 1.
Notes:
- Exit code defaults to 0 if not specified
- All resources are automatically cleaned up before exit
- Alternative keywords:
quit,terminate
Increment/Decrement#
Increment the counter.
Decrement the value.