Statements
Statements
Declarations, blocks, conditionals, the four loop forms, return, and the order in which a script's top-level statements run.
since 0.1.0-alpha.1linuxwindowswasm
Description
Type name = expr; var name = expr; // declaration (var and let infer the type)
Type name; // declaration with the type's default value
using Type name = expr; // deterministic cleanup
expr; // expression statement
{ stmt* } // block
return expr; => expr; // return, and the arrow form of a body
if (cond) stmt [else stmt]
label: while (cond) stmt // labeled loop
do stmt while (cond); // post-test loop
for (init; cond; step) stmt
for (T x in iterable) stmt // ranges, arrays, maps, any iterable
break; continue; // loop control, optionally labeled
throw expr; // expr must implement IException
try stmt catch (Type name?) stmt [catch ...] // the first matching clause runs
bind ...; uses NS; use NS::name (as alias)?; // imports and bindings
; // empty statement
A declaration introduces a variable. Type name = expr; states the type. var and let infer the type from the initializer; they differ only in that a let variable cannot be assigned again. Type name; with no initializer gives the variable the default value of its type: "" for a string, [] for an array, 0 for an int. using declarations are covered by lang.using-statement.
An expression statement evaluates an expression and discards the result.
A block { ... } groups statements and opens a scope: variables declared inside are not visible after the closing brace.
return expr; leaves the enclosing function with a value, and return; leaves a void function. A function body that is a single statement may be written with an arrow, => expr;, where the arrow means return.
if runs the first branch when the condition holds, else the second branch if there is one. The branches are single statements, usually blocks. while tests its condition before each pass and do ... while tests it after, so a do body always runs at least once. for (init; cond; step) is the C-style loop. for (T x in iterable) visits every element of a range (0..2 includes both ends), of an array, of a map (each element is a Pair with first and second), or of any value that implements IIterable<T>.
The statement forms
int twice(int n) => n * 2;
int clamp(int n) {
if (n < 0) return 0;
return n;
}
void inner() {
{
int scoped = 1;
console.writeln(scoped);
}
var inferred = "v";
console.writeln(inferred);
}
int total = 0;
for (int i = 1; i <= 4; i = i + 1) {
total = total + i;
}
console.writeln(total);
int n = 3;
while (n > 0) {
console.write("${n}");
n = n - 1;
}
console.writeln("");
do {
console.writeln("runs once");
} while (false);
for (int k in 0..2) {
console.write("k${k}");
}
console.writeln("");
Array<string> names = ["ann", "bo"];
for (string s in names) {
console.writeln(s);
}
Map<string, int> ages = Map();
ages["ann"] = 30;
for (Pair<string, int> p in ages) {
console.writeln("${p.first}=${p.second}");
}
string empty;
int zero;
console.writeln("[${empty}] ${zero}");
if (total > 5) console.writeln("big"); else console.writeln("small");
console.writeln(twice(4));
console.writeln(clamp(-3));
inner();
10
321
runs once
k0k1k2
ann
bo
ann=30
[] 0
big
8
0
1
v
Rules
- A script's top-level statements run in the order they are written, and function and class declarations may appear between them. There is no automatically called
main; call your own entry function explicitly. - A
letvariable cannot be assigned after its declaration (cannot assign to const). - A body is exactly one statement. Use a block to run several.
for (T x in iterable)accepts a range, an array, a map and anyIIterable<T>. Withvaras the type, the element type is inferred.- The loop-control statements
breakandcontinue, theusingdeclaration, and the exception statementsthrowandtryhave their own entries:lang.loop-control,lang.labeled-loops,lang.using-statementandlang.exceptions.
Examples
Top-level statements run in order
console.writeln("one");
void later() {
console.writeln("later");
}
later();
console.writeln("two");
one
later
two
Notes
varandletdeclarations are accepted inside function bodies and inside the body of a control statement. At the outermost level of a script, declare variables with their type, as the examples above do.- Imports and bindings (
uses,use,bind) are covered with namespaces and with dependency injection.
See also
- Array — An ordered sequence of values of one type,
Array<T>, with value semantics. - Map — An associative collection from keys of type
Kto values of typeV, with value semantics. - break and continue — Leaving a loop early or skipping to its next iteration, and what counts as a loop for these statements.
- Labeled loops — Naming a loop so that break and continue can leave or restart an outer loop from any depth.
- using — deterministic cleanup — A using declaration owns a resource and closes it exactly once on every way out of its block, in reverse order of declaration.
- Exceptions: throw, try and catch — Throwing a value that implements IException, catching it by type, and the standard exception hierarchy.