Switch Case

Navigation:  Language Reference > Statements >

Switch Case

Previous pageReturn to chapter overviewNext page

Syntax

switch variable

 case variable1 do

         equations;

 case variable2 do

         equations;

 default do

         equations;

end;

Example

equations

 z = 4;

 i = floor(time);

 switch i

         case 1 do

                 y = 1;

         case 2 do

                 y = 2;

         case 3 do

                 y = 3;

         case z do

                 y = 4;

         default do

                 y = 5;

         end;

Description

The switch case statement is designed to execute equation(s) based on the value of a variable. Only the equations of the valid branch are executed. If non of the branches is valid, the equations of the default branch are executed.

Limitations

1.The switch variable should not be changed inside one of the branches.
2.Equations within a switch case statement have to be written in the correct order of execution, i.e. they are not rewritten into a causal form but executed sequentially. Therefore it is advised only to use this statement in a code section.

Tips

1.Keep the condition variables (variable1, varable2) constant during simulation to prevent confusion.
2.Make sure that the switch variable has the right value to execute the branch. In the example, the time variable changes value every time step. The floor function is used to keep it at a constant value long enough to see the branches in action.