Control Statements
Statements that control the flow of execution
for
A control statement that loops through a range of numbers of a list of elements
Syntax
The for statement comes in 2 flavours:
Looping through a range of numbers
for varname = expression TO expression (step number)?
(statement)*
next
The optional step operator is the number to iterate by which defaults to 1
Looping through a list of elements
for varname in expression
(statement)*
next
Description
The for statement is used to iterate through numbers or elements and execute some code.
Examples
Iterating through a range of values
// Print out the numbers 1 to 10
for i=1 to 10
print i
next
Iterating through a range of values using a step value
// Print the number 2, 4, 6, 8 and 10
for i=2 to 10 step 2
print i
next
Iteration through values in an array
for i in [1,7,2,10]
print i ^ 2
next
Getting a list of available currencies and iterating through them printing our the currency pair
currencies = find ${currency:public}
for fx in currencies
print fx.currency + "/" + fx.base
next
if
Used to test a boolean condition and take appropriate actions
Syntax
You can use the if structure like the below:
if condition
(statement)*
(elseif condition (statement)*)*
(else (statement)* )?
end
Or you can use a single line assignment conditional expression using a ?
operator:
varname = condition ? true_expression : false_expression
Description
The if statement is a control statement - it controls the flow of the application based on the value of a boolean condition. An if statement can have an multiple elseif blocks which will be sequentially checked to see if there condition is true and an optional else block of statements that execute if the if or elseif condition does not match.
The single line assignment conditional expression can be used to test a condition and assign a value to a variable based on the trueness of the condition. This can also be used in SMART data expressions, e.g.
BASE.size() < 10 ? BASE : OTHER
Examples
A simple condition that checks the value of a variable
if a==1
print "a is 1"
end
A multiple condition if statement
if a==1 and b>5
a=b
end
An example of using an else block that will execute if a is something other than the value 1
if a==1
a=a+1
else
a=a-1
end
An example showing 2 if conditions (if and elseif) and a else block that will execute if either of the if conditions don’t match
if a==1
a=a+1
elseif a>5
a=a+10
else
a=a-1
end
An example showing how to use a single line variable assignment example:
a = score>85 ? "Score is high" : "Score is low"
while
Iterates through some lines of code whilst a condition remains true
Syntax
while condition
(statement)*
end
Description
The while command executes some code whilst a condition remains true
Warning - ensure that the condition can at some point become false, otherwise the code will execute endlessly
Examples
a=99
while a>1
a = a/2
end
throw
Throw a custom exception
Syntax
throw expression
Description
The throw command allows you to throw a custom exception that will return to the calling application.
Examples
throw "Invalid value for category [" + cat + "] must be 'x' or 'y'"