1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
Structure Statements
^^^^^^^^^^^^^^^^^^^^
.. crowbar:element:: StructureStatement <- IfStatement / SwitchStatement / WhileStatement / DoWhileStatement / ForStatement
A structure statement creates some nonlinear control structure.
There are several types of these structures.
.. crowbar:element:: IfStatement <- 'if' '(' Expression ')' Block ('else' Block)?
An if statement allows some action to be performed only sometimes, based on the value of the expression.
Compile-time Behavior:
If the expression does not have type bool, the compiler must emit an error.
Runtime Behavior:
First, the expression is evaluated.
If the expression evaluates to a ``bool`` value of ``true``, then the first block will be executed.
If the expression evaluates to a ``bool`` value of ``false``, either the second block is executed or nothing is executed.
.. crowbar:element:: SwitchStatement <- 'switch' '(' Expression ')' '{' (CaseSpecifier / Statement)+ '}'
CaseSpecifier <- 'case' Expression ':' / 'default' ':'
A switch statement allows many different actions to be taken depending on the value of some expression.
Compile-time Behavior:
The expression must have a type which is either some integer type or an enum.
The expression in a case specifier must have a value which can always be known at compile time, i.e. its value must be a constant or computed from only constants.
Either there must be a case specifier for every valid value in the type of the switch expression, or there must be a default case specifier.
At most one default case may be present.
Runtime Behavior:
First, the switch expression is evaluated.
Whichever case specifier has the same value, or the default case specifier if none is found, is then selected as the matching case specifier.
Any case specifiers immediately following the matching case specifier is ignored.
Subsequent statements are then executed, in linear order, until another case specifier is reached.
The execution of the switch statement then ends.
.. crowbar:element:: WhileStatement <- 'while' '(' Expression ')' Block
Compile-time Behavior:
The expression must have type bool.
Runtime Behavior:
The expression is evaluated, and, if it evaluates to true, the block is executed.
This process repeats until the expression evaluates to false.
.. crowbar:element:: DoWhileStatement <- 'do' Block 'while' '(' Expression ')' ';'
Compile-time Behavior:
The expression must have type bool.
Runtime Behavior:
The block is executed.
Then, the expression is evaluated, and if it is true the process repeats.
.. crowbar:element:: ForStatement <- 'for' '(' ForInit? ';' Expression ';' ForUpdate? ')' Block
ForInit <- ForInitializer (',' ForInitializer)* ','?
ForInitializer <- Type identifier '=' Expression
ForUpdate <- AssignmentBody (',' AssignmentBody)* ','?
Compile-time Behavior:
The individual initializers each have the same behavior as a :crowbar:ref:`VariableDefinition`, but for scope purposes they are treated as though they are inside the block.
The top-level expression in the for statement must have type bool, and will be treated for scope purposes as though it is inside the block.
The update assignments will be treated for scope purposes as though they are inside the block.
Runtime Behavior:
First, the initializers are executed the same way variable definitions would be, in the order they are presented.
Then, the top-level expression is evaluated, and if it is false the for statement ends.
If the top-level expression was true, the block is executed, and then the update assignments are executed in the order they are presented.
The process repeats starting with expression evaluation.
|