if condition then { 1; }; % sugar for `if condition then { 1; } else {;};`
unless condition then { 1; } else { 2; }; % sugar for `if !condition then { 1; } else { 2; };`
Runtime behavior: A conditional if statement has a condition expression, a consequent block (“then” branch), and an optional alternative block (“else” branch). If the condition evaluates to true or a “truthy” value, the consequent is executed; else, the alternative is executed (or if the alternative doesn’t exist, nothing is executed). An unless statement is syntax sugar for an if statement with the condition logically negated.
Short-circuiting: At runtime, exactly one of the consequent or the alternative will be executed, and the branch that is not produced will not even be evaluated (for example, if it contains a procedure call, the procedure will not be called). Additionally, there are plans to include a compiler option that will skip not only evaluation but compilation of that branch, but only if the compiler can determine which branch will be executed based on the condition.
The
if
statement is a control flow statement that executes based on a condition. Theunless
statement is the negation ofif
.Syntax:
Semantics:
Decorate:
Sugar:
Runtime behavior: A conditional
if
statement has a condition expression, a consequent block (“then” branch), and an optional alternative block (“else” branch). If the condition evaluates to true or a “truthy” value, the consequent is executed; else, the alternative is executed (or if the alternative doesn’t exist, nothing is executed). Anunless
statement is syntax sugar for anif
statement with the condition logically negated.Short-circuiting: At runtime, exactly one of the consequent or the alternative will be executed, and the branch that is not produced will not even be evaluated (for example, if it contains a procedure call, the procedure will not be called). Additionally, there are plans to include a compiler option that will skip not only evaluation but compilation of that branch, but only if the compiler can determine which branch will be executed based on the condition.