Language reference · section 16

16. Control flow

Feature: Control flow

  Scenario: if with else
    Given the code "if cond:" with an "else:" block
    Then the first block runs when cond is true, and the second block runs otherwise

  Scenario: else if is a shorthand for an if inside else
    Given the code "else if cond:"
    Then the compiler treats it as an if inside the else branch

  Scenario: elif is an error
    Given the code "elif cond:"
    Then the compiler reports an error "pin has no `elif`. Write `else if` for another condition"

  Scenario: for in iterates values
    Given the code "for x in xs:"
    Then x takes each value of xs in order

  Scenario: for of iterates keys
    Given the code "for k of obj:"
    Then k takes each key of obj

  Scenario: for key, value of iterates keys and values
    Given the code "for key, value of obj:"
    Then key and value take each key and value of obj

  Scenario: while repeats a block
    Given the code "while cond:"
    Then the block runs again while cond is true

  Scenario: break leaves the innermost loop
    Given a break inside a loop
    When the break runs
    Then the loop ends

  Scenario: break outside a loop is an error
    Given a break outside any loop
    Then the compiler reports an error "break is only allowed inside a loop"

  Scenario: A return or a break can end with if
    Given the code "return 0 if n < 2"
    Then the return runs only when n < 2

  Scenario: for await reads an async iterable
    Given a stream from a @worker function
    When the code writes "for await x in stream:"
    Then the loop reads one item at each step