Execution flow

This topic describes how to control the execution flow of an AskiaScript routine. As with all languages, script execution is carried out line by line. By default, every line in a script is interpreted and executed, but, as with all languages, AskiaScript allows you to control the execution flow. You can:

This topic covers:

For / Next statement

In most programming languages, a For loop can be used to repeat a section of code N times, according to the specified StartIndex and EndIndex (in AskiaScript the step is always 1; i.e. the loop counter always goes up by 1 on each iteration). The loop stops repeating when the EndIndex is higher than the specified value, or when the code execution inside the loop reaches a Break, Exit For or Return statement.

Syntax

For [StartIndex] To [EndIndex]
    [Body]
Next

Note:

Examples

' Creates an array with all answers greater than 5
Dim IllegibleBrands = {}  '  Array with answers greater than 5

For i = 1 To brands.AllValues.Count

     If brands.AllValues[i] > 5 Then
        IllegibleBrands.Insert( brands.AllValues[i] )
     EndIf

Next
' Q1 is a numeric question under a loop of loop (ParentLoop and SubLoop)
' Do a sum of Q1 when value is higher than 2

Dim MySum = 0

For i = 1 To ParentLoop.Responses.Count

     For j = 1 To SubLoop.Responses.Count

          If Q1.Iteration(ParentLoop : i, SubLoop : j) > 2 Then
                MySum = MySum + Q1.Iteration(ParentLoop : i, SubLoop : j)
          EndIf

     Next ' Next j

Next ' Next i

↑ Top of page ↑

While / End While statement

Available from version 5.4.2.

In most programming languages, a While loop is used to repeat a piece of code while a given expression is True. The loop will stop repeating when the given expression is evaluated to False, or when the code in the body of the loop reaches a Break, Exit While or Return statement.

Warning: the While loop could produce an infinite loop, and freeze your application. To avoid this. there is a hard-limit of 1,000,000 possible iterations for a loop. This limit will decrease for each level of nested sub-loops.

Also see: SetWhileLimit() / ResetWhileLimit()

 

If an infinite loop is detected during run-time, the process will stop and trigger an exception. For the web: 701: An infinite loop has been detected in the script. See: askiaweb error codes.

 

Syntax

While [Expression]
    [Body]
End While

Note:

Examples

' Create a fibonacci sequence up to 10
Dim i = 2
Dim fib = {0; 1}

While i < 10
  i = i + 1
  fib.Push(fib[i - 2] + fib[i - 1])
End While

Return fib.Join() ' => 0;1;1;2;3;5;8;13;21;34

↑ Top of page ↑

Continue statement

Available from version 5.4.2.

This keyword is used to immediately go to the next iteration of the current For / While loop.

Examples

' Search all index where the value is 5

Dim i
Dim arr = {}
For i = 1 To Q1.Al;Values.Count
    If Q1.AllValues[i] <> 5 Then
         Continue For
    EndIf
    arr.Push(i)
Next

 

' Search all index where the value is 5 

Dim i = 0 
Dim arr = {} 
While i <> Q1.AllValues.Count
   i = i + 1 
   If Q1.AllValues[i] <> 5 Then 
      Continue While 
   EndIf 
   arr.Push(i) 
End While

↑ Top of page ↑

Exit / Break statement

These keywords are used to prematurely break the execution of a scope, such as a For/While loop. After the Exit or Break keyword, the interpreter will jump to the line just after the scope:

The Exit statement is immediately followed by the scope to exit (Exit For or Exit While).

Examples

' Loop until a 5 value is found

Dim i

For i = 1 To Q1.Values.Count
    If Q1.Values[i] = 5 Then
         Exit For
    EndIf
Next

' Here the variable i contains the index of the value 5 

Same as:

' Loop until a 5 value is found

Dim i

For i = 1 To Q1.Values.Count
    If Q1.Values[i] = 5 Then
         Break
    EndIf
Next

' Here the variable i contains the index of the value 5 

 

' Loop until a 5 value is found

Dim i = 0

While i <> Q1.Values.Count
    i = i + 1
    If Q1.Values[i] = 5 Then
         Exit While
    EndIf
End While

' Here the variable i contains the index of the value 5 

Same as:

' Loop until a 5 value is found

Dim i = 0

While i <> Q1.Values.Count
    i = i + 1
    If Q1.Values[i] = 5 Then
         Break
    EndIf
End While

' Here the variable i contains the index of the value 5 

↑ Top of page ↑

If/Then/Else statement

To clarify a condition, or to refine/contextualise the return values (routing Set value), it is possible to use If/Then/Else statements. They can improve the readability of your AskiaScript code.

Syntax and examples

If [Condition] Then
     [Body]
EndIf
If [Condition] Then
     [Body]
Else
     [Body]
EndIf
If [Condition] Then
     [Body]
ElseIf [Condition] Then
     [Body]
Else
     [Body]
EndIf

 

If Gender = 1 Then    ' => (Gender = 1) is a boolean expression: True when the response of the question Gender is 1
      Return True 
EndIf
If Gender Then    ' => If the question Gender have a value different than "", {}, 0 or False
      Return True 
EndIf
Dim text
If Gender  = 1 And Age < 30 Then
      text = "Young man"  ' => Affectation
EndIf
If Gender  = 1 And Age < 30 Then
      Return True  ' => Return value
EndIf

↑ Top of page ↑

Return statement

The evaluation of a routing condition is done on the latest expression or on the expression returned with the return statement. When a return expression is reached, it immediately breaks the script flow (all subsequent lines in the script are ignored) and returns the associated expression.

Examples

' Selection using Gender x Age ...
If Gender = 1 And Age = 1 Then
   return True  ' Immediately return True
ElseIf Gender = 1 And Age = 2 Then
   return False ' Immediately return False    
EndIf 

'... otherwize selection using profession
If Profession = 3 Then
   return True
EndIf

'... all other cases return Undefined (so treat as False)

↑ Top of page ↑

GoTo statement

Available from version 5.3.5.

The GoTo statement gives you a way to explicitly specfiy the next line of code to execute. After the GoTo statement, the process will ignore all next lines until it finds the label associated with the GoTo. It can be very useful when sharing a section of code between multiple If/Else conditions, or to simply avoid the execution of certain code.

Syntax/Examples

GoTo [Label]

[CodeToSkip]

[Label]:

[CodeToExecute]
dim something = "" 

If q1 Has {1} Then
   something = "Q1" 
   GoTo SomethingHas1
Else If q2 Has {1} Then
   something = "Q2" 
   GoTo SomethingHas1
EndIf   

' This part of code is not executed if some questions has {1}
If q1 Has {2} Then
   something = "Q1" 
   GoTo SomethingHas2
Else If q2 Has {2} Then
   something = "Q2" 
   GoTo SomethingHas2
EndIf

' This part of code is not executed if q1 and q2 has not 1 and 2
something = "Not found" 
GoTo Result

' This part of code is executed only when q1 or q2 has {1}
SomethingHas1:
 something = something + " HAS {1}" 
 GoTo Result

' This part of code is executed only when q1 or q2 has {2} and not has {1}
SomethingHas2:
 something = something + " HAS {2}" 
 GoTo Result

' This part of code is always executed
Result:

Return "The result is '" + something + "'" 

The previous code will produce the following output:

↑ Top of page ↑

Label

Available from version 5.3.5.

A label is use to localise a section of code.

Declaration

Indicates the name of the label immediately follows by colon ( : )

The name of the label must obey the following rules:

Valid label:

MyLabel:

Invalid label (the space between the name and the colon ( : ) is not allowed):

MyLabel :

Exceptions

The label 'name_of_label' is already defined

↑ Top of page ↑

Create your own Knowledge Base