The CSL language supplies statements for conditional execution and iterative executions of statements/equations.  These are described below.

### Conditionals (IF/ELSE)

Conditionals in the CSL language are used to indicate the conditions under which certain statements/equations should be evaluated:

• Each branch of the conditional is denoted by the IF keyword, followed by a parenthesized logical expression (typically containing logical and/or relational operators)
• Alternative branches are denoted by the ELSE keyword, followed by another IF expression
• A default alternative is denoted by the final ELSE keyword in the overall conditional statement; the fact that the ELSE statement is not followed by another IF expression indicates that this is a default branch.
• The overall statement is terminated by the END keyword.  Note that this differs from legacy implementation of the CSL language, in which a conditional was terminated with the ENDIF keyword.


model ConditionalExample

derivative

! Conditionals are defined by sections of statements
! contained within "if", "else if" and  "else" clauses.
! The conditional statement is now terminated with the
! "end" keyword
if (t < 1.0)
x = t^2
else if ( t < 1.35)
x = exp(t)
else if (t < 7.29)
x = sin(t)
else
x = sqrt(t)
end

constant tstop = 10.0
termt(t >= tstop, 'Stopped on time limit')

end ! derivative

end ! program


### FOR Loops

Iterating over a set of statements/equations (which typically involve array or matrix variables/constants) is accomplished with the FOR statement:

• The set of equations to be iterated over is delimited by the initial FOR statement and the enclosing END statement
• The FOR statement requires at least two arguments: the limits over which iterations should take place, and an optional step size.
• The iterator of the for statement can be an integer data type (used, for example, to index elements in an array), or a floating point value.  The example below uses and integer value, which is declared via the DIMENSION statement just above the loop.
• For sitting purposes, the FOR loop is treated as a single statement; overall input and output dependencies are automatically detected by Magnolia.  No sorting is performed within the FOR loop itself.
• If the FOR loop is not sorted as intended by default, it can be wrapped in a PROCEDURAL section to enforce an alternate sorting with respect to other statements in the model.
• FOR loops may be nested, and are allowed in any code section.


model LoopExample

initial

! Declare an array that will be iterated over
! by a for loop
! Note that arrays are now declared and indexed using
! square brackets, not parentheses
dimension resetTimes[5]
constant resetTimes = 1.1, 2.8, 3.8, 4.93, 7.4343
constant ntimes = 5

! The end of a loop statement is now denoted with the "end"
! keyword.  Labeled statements and "continue" are no longer
! supported.

! The loop index shold be declared if it's an integer
dimension integer i
for i = 1, ntimes
schedule reset at resetTimes[i]
end

end

derivative

constant k = 1.0
xd = k
x  = integ(xd, 0.0)

constant tstop = 10.0
termt(t >= tstop, 'Stopped on time limit')

end ! derivative

discrete reset
x = 0.0
end

end ! program