If Then Else (expression)

Navigation:  Language Reference > Statements >

If Then Else (expression)

Previous pageReturn to chapter overviewNext page

Syntax

y = if condition then

 expression

else

 expression

end;

Description

The if-then-else expression evaluates a condition and makes y equal to the first expression if the condition is true and makes y equal to the second expression if the condition is true. If the condition is true, 20-sim ignores the second expression. If the condition is false, 20-sim ignores the first expression. Because the complete construct forms one expression, only one semicolon is used at the end.

Nesting

If-then-else expression may be nested. Take care that no semicolons are used, because no matter how large the construct is, it is still one expression!

Examples

y = if time > 5 then

         -ramp(1)        // statement to be assigned to y if condition is true,

 else

         ramp(1)        // statement to be assigned to y if condition is false,

 end;

 

// Nesting:

x = if time < 1 then

         1

 else

         if time < 2 then

                 2

         else

                 if time == 2 then

                         1

                 else

                         0

                 end

         end

 end;

Limitations

1.The output of the condition must be a boolean.
2.If needed, always use the equal operator (==) in the condition.

Note

1.Take care when using event functions in if-then-else expressions. In if-then-else expressions only the equations of the true parts are evaluated, so event functions may not always be triggered!
2.There is also an if-then-else statement.
3.Equations within an if statement have to be written in the correct order of execution, i.e. they are not rewritten into a causal form but executed sequentially.