Skip to main content

Control Flow

Stof's control flow looks like JavaScript or C, with a few additions.

if and else​

Braces are optional when a branch is a single statement:

if-else.stofPlayground ↗
#[main]
fn main() {
  const usage = 7;
  if (usage > 10) pln('over the limit');
  else if (usage > 5) pln('close to the limit');
  else pln('fine');
}
Output

if is a statement. For a value that depends on a condition, use the ternary operator: const label = usage > 10 ? 'over' : 'fine';.

switch​

A switch runs the first case that matches. Cases don't fall through, except that an empty case shares the next one's body. A switch can also be an expression that gives a value:

switch.stofPlayground ↗
#[main]
fn main() {
  const plan = 'team';

  switch (plan) {
      case 'free': pln('no seats');
      case 'pro':
      case 'team': pln('paid plan');
      default: pln('unknown plan');
  }

  const seats = switch (plan) {
      case 'pro': 1
      case 'team': 10
      default: 0
  };
  pln(seats);
}
Output

while and loop​

loop repeats until a break:

while.stofPlayground ↗
#[main]
fn main() {
  let remaining = 3;
  while (remaining > 0) remaining -= 1;
  pln(remaining);

  let tries = 0;
  loop {
      tries += 1;
      if (tries == 3) break;
  }
  pln(tries);
}
Output

for​

for...in loops over a list, a map, a set, or a number (from 0 up to it). The C-style loop works too:

for.stofPlayground ↗
#[main]
fn main() {
  for (const name in ['Ada', 'Grace']) pln(name);
  for (let i in 3) pln(i);

  let total = 0;
  for (let i = 0; i < 5; i += 1) total += i;
  pln(total);
}
Output

Inside a for...in loop, index is the current position, and first and last say whether this is the first or last item:

for-in.stofPlayground ↗
steps: ['parse', 'validate', 'run']

#[main]
fn main() {
  for (const step in self.steps) {
      const marker = first ? 'start' : (last ? 'end' : 'then');
      pln(`${index}: ${marker} ${step}`);
  }
}
Output

Loops can take items apart as they go: for (const { name, plan } in users) or for (const (key, value) in map). See Destructuring. To loop over an object's fields, use for (const (name, value) in obj.fields()).

break, continue, and Labels​

continue skips to the next pass, and break ends the loop. Label a loop with ^name to break out of it from inside a nested loop:

labels.stofPlayground ↗
#[main]
fn main() {
  let found = null;
  ^search for (const row in 3) {
      for (const col in 3) {
          if (row * col == 2) {
              found = (row, col);
              break ^search;
          }
      }
  }
  pln(found);
}
Output

Without the label, break would only end the inner loop.

try and catch​

throw raises an error, with any value: a string, a number, an object. catch handles it, and catch (e: type) only catches errors of that type:

try-catch.stofPlayground ↗
fn charge(amount: float) {
  if (amount <= 0) throw('amount must be positive');
}

#[main]
fn main() {
  try {
      self.charge(-5);
  } catch (message: str) {
      pln(`declined: ${message}`);
  }
}
Output

Error Handling goes further. Next, Collections.