Statements
You can structure the FPScript code for your formula in one or more statements. Normally, you write every statement on its own line. You can, however, also write several statements on the same line. These have to be separated with a semicolon ';'. You can also split a statement over several lines. For lines which are to be merged with the lines following, you have to enter a backslash '\' or an underscore character '_' as the final character.
Assignment
You can use an assignment to assign different content to a variable. In a variable containing, for instance, a data series, you can specifically overwrite individual values or sections using the Indexed Assignment. With the Set statement or the As Object keyword you can assign an object reference to a variable. You can then use this variable to access the properties of the referenced object.
Making Decisions with If...Then...Else
The If...Then...Else statement is used to check whether a condition has the value TRUE or FALSE, in order to execute one or more statements conditionally. Normally, the condition is an expression with a comparison operator for comparing two variables or values. You can find information on comparison operators under "Comparison Operators". If...Then...Else statements can be nested to any depth.
Executing a Block When a Condition Results in TRUE
In this case, the keyword Else is omitted:
If Value > 2 Then
Value = 5
End
Executing One Block When a Condition is TRUE and Another When the Condition is FALSE
The statements to be executed if the condition results in FALSE are inserted between the keywords Else and End.
If Value > 2 Then
Value = 5
Else
Value = 0
End
Using Loops to Repeat Code
FPScript offers a host of operations to search for events and to process data sets in their entirety. In rare cases, however, you will have to use loops to run across the values in a data series, for instance. With a loop, you usually define a loop variable that enumerates the repetitions. This variable can then be used in an index operation, for instance, to access the corresponding value in a data set:
// Declare variable
Dim Result
// Copy data series to be calculated
Result = DataSeries
// Run across all values in the data series
For Each Row i In DataSeries Do
// Set values less than zero to zero in the result
If DataSeries < 0 Then
Result = 0
End
End
Return Result // Pass the result data series
The above example uses a loop to find a negative value in a data series and to set it to zero. All statements between Do and End are executed several times, where the variable i counts from 0 to the number of values in the data series of data series minus 1. The example also shows a conditional statement. The statement Result = 0. is only carried out if the ith value in DataSeries is less than zero. This example also shows how Indents can make FPScript programs easier to read. All statements within the loop were indented by one tab position. The same applies to the conditional statement.
Note: You should only use loops if no appropriate function exists and if the problem cannot be described with an arithmetical expression. The integrated functions of FlexPro are many times faster than any loops programmed using FPScript. The example above can, for instance, be re-written as follows:
Dim Result, Idx // Declare variable
Result = DataSeries // Copy data series to be calculated
// Determine position of all values < 0
Idx = ValuesAboveLevel(DataSeries, 0, EVENT_INDEX + EVENT_COMPLEMENT)
// Set values at these positions in the result to zero
Result[Idx] = 0
Return Result // Pass the result data series
You can use the Break statement to cancel a loop prematurely:
Dim Result = 0
For Each Value v In DataSeries Do
If v == ? Then
Break
End
Result = Result + v
End
Return Result
FPScript offers you the following loops:
While...End: Executes a loop while the condition results in the value TRUE.
Do...While: Executes a loop until a condition results in the value FALSE.
For...End: Uses a counter to repeat the statements as often as is specified by the counter.
For Each Value...End: Traverses all values of a data set. No counter is used.
For Each Element...End: Uses a counter to iterate all of the values in a data series or a data matrix.
For Each Row...End: Uses a counter to count all of the items in a list.
For Each Column...End: Uses a counter to iterate all of the columns in a data matrix.
Repeating Statements While a Condition is TRUE
The While...End loop first tests the condition and then executes the statement as long as the condition results in TRUE. The following example searches for the position in DataSet, where the first positive value occurs.
i = 0
While DataSet < 0 Do
i = i + 1
End
Repeating Statements Until a Condition is FALSE
The Do...While loop first executes the statement one time and then tests the condition. The following example calculates random numbers until a number greater than or equal to zero is generated.
Dim Z
Do
Z = Noise(0)
While Z < 0
Using "For...End"
You can use For...End statements to repeat a block of statements as often as is specified. In loops, use a counter variable with a value that is increased each time the loop is executed.
Dim a = "" # 3
For i = 0 To 2 Do
a = TextInput("Enter text here")
End
Using "For Each Row...End"
If, as in the example above, you want to iterate across all elements of a data set, then you can implement this more elegantly with the For Each Row...End loop:
Dim a = "" # Input("Count")
For Each Row i In a Do
a = TextInput("Enter text here")
End