If Then Else

Navigation:  Language Reference > Statements >

If Then Else

Previous pageReturn to chapter overviewNext page

Syntax

if condition then

 equation;

 ...

else

 equation;

 ...

end;

Description

The if-then-else statement evaluates a condition and performs the first set of equation(s) if the condition is true and the second set of equation(s) if the condition is false. If the condition is true, 20-sim ignores the second set of equation(s). If the condition is false, 20-sim ignores the first set of equation(s).

Nesting

If-then-else statements may be nested as many times as desired. Do not forget to included the word "end;" to finish every nested statement!

Examples

y = step(1);

u = -step(1);

if time > 5 then

 y = -ramp(1);        // equations to be executed if condition is true,

 u = ramp(1);        // these are not executed if condition is false

else

 y = ramp(1);        // equations to be executed if condition is false,

 u = -ramp(1);        // these are not executed if condition is true

end;

 

//Nesting

if time < 5 then

 a = 1;

 b = sin(time*1);

else

 if time == 5 then

         a = 2;

         b = sin(time*2);

 else

         a = 3

         b=sin(time*3);

 end;

end;

Limitations

1.The output of the condition must be a boolean.

Note

1.Take care when using an event functions in if-then-else statements. In if-then-else statements 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 expression.
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.