An expression statement simply consists of an expression (see Clause 8) followed by a semicolon.
Examples
currentOffer = this.offers[1];
monitor.SignalAlarm(sensorId);
this.interest = this.principal * this.rate * period;
Syntax
ExpressionStatement(s: ExpressionStatement)
= Expression(s.expression) ";"
Figure 9 49 Abstract Syntax of Expression Statements
Cross References
-
Expression see Subclause 8.1
-
Statement see Subclause 9.1
Semantics
An expression statement is executed by evaluating the expression (see Clause 8). If the expression produces one or more values, these are discarded.
The values assigned to any local names within an expression statement (e.g., via an assignment expression—see Subclause 8.8) may be accessed after the execution of the statement by using the local names in name expressions (see Subclause 8.3.3).
9.8if Statements
An if statement allows for the conditional execution of one of a set of blocks of statements. The conditional blocks are organized into sequential sets of concurrent clauses. Each clause in a concurrent set includes a conditional expression (which must be of type Boolean) and a block to execute if that condition is true. An if statement may also optionally have a final clause with a block to execute if no other conditions are true.
NOTE. The notation for concurrent clauses is not available at the minimum conformance level (see Subclause 2.1).
Examples
if (reading > threshold) {
monitor.raiseAlarm(sensorId);
}
//@determined @assured
if (reading <= safeLimit) {
condition = normal; }
or if (reading > safeLimit && reading <= criticalLimit) {
condition = alert; }
or if (reading > criticalLimit) {
condition = critical; }
Syntax
IfStatement(s: IfStatement
= "if" SequentialClauses(s) [ FinalClause(s.finalClause) ]
SequentialClauses(s: IfStatement)
= ConcurrentClauses(s.nonFinalClauses)
{ "else" "if" ConcurrentClauses(s.nonFinalClauses) }
ConcurrentClauses(c: ConcurrentClauses)
= NonFinalClause(c.clause) { "or" "if" NonFinalClause(c.clause) }
NonFinalClause(c: NonFinalClause)
= "(" Expression(c.expression) ")" Block(c.block)
FinalClause(b: Block)
= "else" Block(b)
Figure 9 50 Abstract Syntax of if Statements
Cross References
-
Expression see Subclause 8.1
-
Statement see Subclause 9.1
-
Block see Subclause 9.1
Semantics
Each set of concurrent clauses is executed in sequence until a condition evaluates to true. Within each set, all the conditional expressions are evaluated in parallel. If any condition evaluates to true, then the associated block may be executed. If more than one condition evaluates to true, then one associated block is chosen non-deterministically to execute. If no conditions evaluate to true, execution proceeds with the next set of concurrent clauses.
Sequential Clauses
In its simplest form, an if statement has a single condition which determines whether or not a single block is executed. For example, in
if (reading > threshold) {
monitor.raiseAlarm(sensorId);
}
the invocation is executed only if the condition reading > threshold is true. A final clause may be added, as in
if (reading > threshold) {
monitor.raiseAlarm(sensorId);
}
else {
monitor.logReading(sensorId, reading);
}
In this case, the logReading operation is called if reading > threshold is false.
An if statement may also have a list of conditional clauses that are tested sequentially. For example,
if (reading <= safeLimit) {
condition = normal;
}
else if (reading <= criticalLimit) {
condition = alert;
}
else {
condition = critical;
}
Concurrent Clauses
Clauses beginning with or instead of else are tested concurrently rather than sequentially. For example,
if (reading <= safeLimit) {
condition = normal;
}
or if (reading > safeLimit && reading <= criticalLimit) {
condition = alert;
}
or if (reading > criticalLimit) {
condition = critical;
}
Note that the second condition has the added test reading > safeLimit, since the first condition will no longer be evaluated sequentially before the second. If this addition had not been made, then, if, in fact, reading <= safeLimit was true, both of the first two conditions could be true, and either of the associated blocks might actually execute (but only one would).
Sequential and conditional clauses can also be mixed, but sets of contiguous concurrent clauses are always sequenced together. Thus, in
if (reading <= safeLimit) {
condition = normal; }
else if (reading > safeLimit && reading <= criticalLimit} {
condition = alert; }
or if (reading > criticalLimit && reading < errorLimit) {
condition = critical; }
else {
condition = error; }
if the first condition is false, then both the next two conditions are evaluated concurrently. If both these conditions are also false, then the final clause is executed.
Annotations
The annotations @assured and @determined may be used with an if statement (see also Subclause 9.2 on annotations). The annotation @assured indicates that at least one condition in the if statement will always evaluate to true. The annotation @determined indicates that at most one condition will evaluate to true. The annotations may be used together, which indicates that exactly one condition will always evaluate to true.
Names
New local names may not be defind in conditional expressions, since these may not always be evaluated, but existing local names may be reassigned. However, the same name may not be assigned in more than one conditional expression within the same concurrent set of clauses, because these expressions are evaluated concurrently, so assignments of the same name in more than one of them could potentially conflict. Assignments made in the conditional expression of a non-final clause are available in the block of that clause.
New local names may only be defined in the clause blocks of an if statement that has a final else clause and then only if the same names are defined in every clause. The type of such names after the if statement is the effective common ancestor (see the definition in Subclause 8.7) of the types of the name in each clause with a multiplicity lower bound that is the minimum of the lower bound for the name in each clause and a multiplicity upper bound that is the maximum for the name in each clause.
Share with your friends: |