Skip to content
Cameron Purdy edited this page Apr 4, 2020 · 6 revisions

A while statement is a looping statement; a looping statement allows some portion of code to be executed repeatedly. Specifically, a while statement allows the conditional execution of a block of code, zero or more times.

The execution of an while statement begins with the evaluation of the ConditionList (one or more Condition), which yields a Boolean value, and that value controls whether the body of the loop will be executed. For a single Condition:

  • The Condition can be an expression. In this case, the expression must yield at least one value, and the first value must be of type Boolean, which will be used as the result of the Condition. Any additional values are silently discarded.
  • Alternatively, the Condition can use an OptionalDeclaration (or OptionalDeclarationList), which defines one or more assignments that result from an expression. The expression must yield at least two values, and the first value must be of type Boolean, which will be used as the result of the Condition.
  • In either case, if the Condition expression short-circuits, then False will be used as the result of the ConditionList.

For a ConditionList with more than one Condition, evaluation begins with the first Condition. If the Condition evaluates to False or short-circuits, then the evaluation of the ConditionList completes at that point with the value False. If the Condition evaluates to True, then evaluation proceeds to the next Condition; if every Condition, in turn, evaluates to True, then the ConditionList evaluates to True.

If the ConditionList evaluates to True, then the loop body is executed; otherwise, the while statement completes. When the body completes (if it completes), then the while statement executes from the beginning (it loops).

Execution of a break statement that applies to the while statement causes the while statement to complete.

Execution of a continue statement that applies to the while statement causes the while statement to execute from the beginning.

A while statement labeled by a LabeledStatement provides two read-only label variables that expose the state of the loop:

Name Type Description
first Boolean True iff this is the first iteration of the loop
count Int The count of completed iterations of the loop

These label variables are explicitly name-scoped using the label name; for example:

PrintNames:
while (String name := nameIterator.next())
    {
    if (!PrintNames.first)
        {
        console.print(", ");
        }
    console.print(name);
    }

The ConditionList is reachable if the while statement is reachable; the ConditionList completes if it is reachable and the expression completes or short-circuits. The statement block is reachable if the ConditionList expression completes (i.e. if the ConditionList can complete without the expression short-circuiting) and the value of the ConditionList is not the constant False. The while statement completes if the ConditionList completes and is not the constant True, or if a break statement that applies to the while statement is reachable.

Definite assignment rules:

  • The VAS before the ConditionList is the VAS before the while statement.
  • The VAS before the statement block is the VAST after the ConditionList.
  • If the ConditionList can short-circuit, then the VAS at each possible point of short-circuiting is joined with the VASF after the ConditionList.
  • The VAS after the while statement is the VAS after the statement block joined with the VASF after the ConditionList.

The while statement provides a local variable scope for any declarations in the ConditionList; that scope exists to the end of the while statement, which is to say that the statement block is nested within that scope. The statement block naturally provides a local variable scope, because it is a statement block.

    WhileStatement:
        while ( ConditionList ) StatementBlock

From IfStatement:

    ConditionList:
        Condition
        ConditionList , Condition

    Condition:
        Expression
        OptionalDeclaration ConditionalAssignmentOp Expression
        ( OptionalDeclarationList , OptionalDeclaration ) ConditionalAssignmentOp Expression

    ConditionalAssignmentOp:
        :=
        ?=

And from VariableStatement:

    OptionalDeclarationList:
        OptionalDeclaration
        OptionalDeclarationList , OptionalDeclaration

    OptionalDeclaration:
        Assignable
        VariableTypeExpression Name

    VariableTypeExpression:
        val
        var
        TypeExpression

    Assignable:
        Name
        TernaryExpression . Name
        TernaryExpression ArrayIndexes