# Differences between the 3 types of IF statements and examples

Pedro Cobas. EcosimPro Development Team

In EcosimPro‘s EL Language, different options are available to the user for marking the conditionality of an expression when modelling components. Doubts sometimes arise about these different options and how to use them.

EL possesses the classic sequential type structure IF-THEN-ELSE that can be used in sequential blocks such as the INIT of the components and the BODY of the functions defined in EL. Its purpose is to select a sequence of assignments for execution, depending on the value of a boolean. The syntax is traditional:

IF condition THEN
seq_stm_s
( ELSEIF condition THEN seq_stm_s ) _
( ELSE seq_stm_s )
END IF

where condition is the condition being evaluated and seq_stm_s are the instructions that are going to be executed. During the run, the conditions are evaluated sequentially and if their value is TRUE, the corresponding instructions are executed. These kinds of structures can be nested and the conditions can contain logical expressions that use the logical operators AND, OR or expressions of the type less than or equal to (<=), greater than or equal to (>=), etc.

Below is a example of its use:

BODY
IF ( signal == SIN) THEN
value = s in (2 * 3.14159 *x )
ELSEIF ( signal == SQUARE) THEN
value = isqr
ELSE
value = 1 .
END IF

A second type of IF is used in the CONTINUOUS block of the components and ports. In this case, an equation can be changed dynamically according to a condition. The mathematical model considers all of the branches defined and depending on the value that the condition takes during the simulation, will use one or another during the time that the condition is matched.

COMPONENT if_example
DECLS
REAL x= 3
ENUM modes switchMode= DESIGN
CONTINUOUS
x= IF ( switchMode == DESIGN ) 5 . 6
ELSEIF ( switchMode== TRANSIENT ) sin (TIME)
ELSE sin (TIME) + cos (TIME)
END COMPONENT

There is a third type of IF in the continuous block that allows introducing one set of equations or another (in the previous IF, this was done on the individual equation level). This is IF-INSERT. A typical case here would be a component with a parameter that determines whether a simpler or more complicated mathematical model should be used: the parameter determines which equations are taken for the simulation and which are discarded.
The variables that can be checked in this IF have to be construction parameters, because the configuration of the component has to be known beforehand.
In the following example, IF-INSERT is used to decide whether the x and y variables are calculated analytically or are read from a table depending on the value of the use Interpolation boolean parameter. As a function of the value of the condition, one block or another of equations will be introduced in the mathematical model.

COMPONENT example (BOOLEAN useInterpolation= TRUE)
DECLS REAL x
REAL y
TABLE_1D t1 = { { 0 , 1 } , { 0 . 2 3 , 0 . 8 9 } }
CONTINUOUS
IF ( useInterpolation == TRUE) INSERT
x= linear Interp1D ( t1 , TIME)
z= linear Interp1D ( t1 , TIME+1)
ELSE
x= cos ( y )**2
z= sin ( y )**2
END IF
y = sqr t (TIME)
END COMPONENT

This clears the way for the creation of components of variable complexity depending on the type of analysis to be performed.