Declare equations or execute statements conditionally

#### Examples

#### If clause

**parameter** Boolean linear=true;
**parameter** Boolean quadratic=false;
Real x, y;
**equation**
**if** linear **then**
y = x + 1;
**elseif** quadratic **then**
y = x^2 + x + 1;
**else**
y = Modelica.Math.sin(x) + 1;
**end if**;

#### If expression

Integer i;
Integer sign_of_i=**if** i<0 **then** -1 **else if** i==0 **then** 0 **else** 1;

#### Syntax

In equation sections:

**if** expression **then**
{ equation ";" }
{ **elseif** expression **then**
{ equation ";" }
}
[ **else**
{ equation ";" }
]
**end if**

In algorithm sections:

**if** expression **then**
{ algorithm ";" }
{ **elseif** expression **then**
{ algorithm ";" }
}
[ **else**
{ algorithm ";" }
]
**end if**

#### Description

#### If clause

The expression of an **if** and **elseif**-clause must be scalar Boolean expression.
One **if**-clause, and zero or more **elseif**-clauses, and an optional **else**-clause
together form a list of branches. One or zero of the bodies of these **if**-, **elseif**-
and **else**-clauses is selected, by evaluating the conditions of the **if**- and
**elseif**-clauses sequentially until a condition that evaluates to true is found. If none of
the conditions evaluate to true the body of the **else**-clause is selected (if an
**else**-clause exists, otherwise no body is selected). In an algorithm section, the selected
body is then executed. In an equation section, the equations in the body are seen as equations
that must be satisfied. The bodies that are not selected have no effect on that model evaluation.

If clauses in **equation** sections which do not have exclusively parameter expressions as
switching conditions shall have an **else** clause and each branch shall have the *same
number* of *equations*. *[If this condition is violated, the single assignment rule
would not hold, because the number of equations may change during simulation although the number
of unknowns remains the same].*

#### If expression

An expression

**if** expression1 **then** expression2 **else** expression3

is one example of if-expression. First expression1, which must be Boolean expression,
is evaluated. If expression1 is true expression2 is evaluated and is the value of the
if-expression, else expression3 is evaluated and is the value of the if-expression.
The two expressions, expression2 and expression3, must be type compatible and give the
type of the if-expression. If-expressions with **elseif** are defined by replacing
**elseif** by **else if**.*[Note: elseif is added for symmetry with if-clauses.]*

Generated at 2024-08-10T18:16:01Z
by OpenModelicaOpenModelica 1.23.1 using GenerateDoc.mos