If Then Elsif Else

Navigation:  Language Reference > Statements >

If Then Elsif Else

Previous pageReturn to chapter overviewNext page

Syntax

if condition then

 equation;

 ...

elsif condition then

 equation;

 ...

elsif condition then

 equation;

 ...

else

 equation;

 ...

end;

Description

The if-then-elsif-else statement is a variant of the if-then-else statement. It makes nested conditions easier to write. It evaluates a condition and performs the first set of equation(s) if the condition is true, otherwise it evaluates the second condition and performs the second set of equation(s) if that condition is true and so on. If the first condition is true, 20-sim ignores the other sets of equation(s). If the first condition is false, 20-sim ignores the first set of equation(s) and jumps to the second condition etc.

Examples

x = (integer) time;

if x < 4 then

 y = 1;

elsif x < 6 then

 y = 3;

elsif x < 8 then

 y = 5;

else

 y = 0;

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