# when-Statements

## 1 General Description

When-statements are used to express actions (statements) that are only executed at events, e.g. at discontinuities or when certain conditions become true. The executed statements contain discrete-time expressions since they are computed only at event instants, even expressions that otherwise would be classified as continuous-time.

When-statements are generally used to either reinitialize a continuous variable using the reinit operator or to change the value of a discrete variable. When-statements are closely related to when-equations, the obvious difference between them is that
when-statements are statements containing other statements and can only be used in algorithm sections whereas when-equations are equations which are only allowed in equation sections.

## 2 WhenStat

The when-statement below are executed at the event instant when the Boolean expression x > 2 becomes true. We should note that this is different from an if-statement with the same condition which executes its body each time when invoked provided that the condition of the if-statement is true. This difference is easier to understand if we consider the definition of when-statements in terms of if-statements and the edge operator in the section below.

## 4 WhenStat2

If we use a Boolean vector expression containing three elements as the conditions, then the two statements will be executed at event instants when at least one of the three conditions: x > 2, sample(0, 2) or x becomes true. Typically each condition in the vector generates its own event instants.

## 6 WhenStat

The order between the statements in the body of the when-statement is of cuorce significant. If you would put the assignment to y3 before the assignment of y1 you would get a different result and thus a different model behavior.

## 8 Defining when-Statements by if-Statements

A when-statement can be defined as a combination of the following:

# - Calls to the edge operator in the conditional expressions of the if-statement

## 9 Avoiding Multiple-Definition Conflicts for when-Statements

Two when-statements in different algorithm sections or two when-equations may not define the same variable. Without this rule a conflict between the assignment will occur if both conditions would become true at the same time instant.

### 9.1 DoubleWhenSequential

One possible way to resolve the conflict would be to put both when-statements within the same algorithm section, as in the DoubleWhenSequential model below. The sequential execution semantics of algorithm sections allow several assignments to the same variable. If there are several when-statements with assignments active at the same time instant, they will be executed in sequential order and the result of the last assignment will prevail.

However, this method is not recommended since the modeling style is less clear than the alternative method presented in the next section. Another disadvantage is the nondeclarative style of using
when-constructs by relying on multiple sequential assignments.

### 9.3 WhenPriority

A more explicit and therefore better way to resolve the conflict would be to give one of the two event handlers higher priority, which is possible using the
elsewhen construct. Here it is well defined what happens if both conditions become true at the same time instant since the first condition (x >= 5) has higher priority than the second condition (x ).