-
Notifications
You must be signed in to change notification settings - Fork 21
do statement
The do
statement is very similar to the while
statement, except that it always executes its body (the statement block) at least one time.
Execution of the do
statement begins with the execution of the body. After the execution of the body, the do
statement evaluates the ConditionList, which controls whether the loop will be repeated. If the ConditionList evaluates to True
, then the do
statement executes from the beginning (it loops); otherwise, if the ConditionList evaluates to False
or short-circuits, then the do
statement completes.
Execution of a break
statement that applies to the do
statement causes the do
statement to complete.
Execution of a continue
statement that applies to the do
statement causes the do
statement to advance its execution to the ConditionList.
Like the while
statement, a do
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 |
The statement block is reachable if the do
statement is reachable. The ConditionList is reachable if the statement block completes, or if a continue
statement that applies to the do
statement is reachable; the ConditionList completes if it is reachable and completes or short-circuits. The do
statement completes if the ConditionList completes and is not the constant True
, or if a break
statement that applies to the do
statement is reachable.
Definite assignment rules:
- The VAS before the statement block is the VAS before the
do
statement. - The VAS before the ConditionList is the VAS after the statement block.
- The VAS after the
do
statement is the VAS after the ConditionList.
The do
statement provides a local variable scope that includes both the statement block and the ConditionList; this scope is used by the statement block in lieu of its own local variable scope, such that variables declared within the statement block are still in scope when the ConditionList executes. This unusual arrangement allows the loop’s ConditionList to reference information otherwise available only within the statement block.
DoStatement: do StatementBlock while ( ConditionList )
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