## 'if'if |

This information is part of the Modelica Standard Library maintained by the Modelica Association.

Declare equations or execute statements conditionally

parameterBoolean linear=true;parameterBoolean quadratic=false; Real x, y;equationiflineartheny = x + 1;elseifquadratictheny = x^2 + x + 1;elsey = Modelica.Math.sin(x) + 1;end if;

Integer i; Integer sign_of_i=ifi<0then-1else ifi==0then0else1;

In equation sections:

ifexpressionthen{ equation ";" } {elseifexpressionthen{ equation ";" } } [else{ equation ";" } ]end if

In algorithm sections:

ifexpressionthen{ algorithm ";" } {elseifexpressionthen{ algorithm ";" } } [else{ algorithm ";" } ]end if

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].*

An expression

ifexpression1thenexpression2elseexpression3

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.]*