This article will cover the syntax of the ‘for… next’ control script statement.
This is an iterative statement where a counter is specified. The statement will execute for each value of the counter.
Statement syntax
For counter = expr1 to expr2 [ step expr3 ] [statements]
[exit for [ ( when | unless ) condition ] [statements]
Next [counter]
Counter | This is the variable name of the counter used. |
Expr1 | This is the first value of the counter. The statement will use this value in the first loop. |
Expr2 | This is the last value of the counter variable. This is the last value that will be used in the loop. |
Expr3 | This is an expression specifying how the counter should be incremented after each loop. |
Statements | Qlik Sense statements to be executed inside the loop. |
Exit for | An ‘exit for’ clause can be used to exit the statement. The execution will shift to the first statement after this control statement. |
When | Unless | The when/ unless clause can be used with the ‘exit for’ clause to make the exit conditional. |
Condition | The conditional statement to be evaluated by the when/ unless clause. The expression must evaluate to True or False. |
Example
This example creates a daily calendar for the whole of 2021.
Set a = '01/01/2021';
For counter = 1 to 365 step 1
CALENDAR:
LOAD * INLINE [
Date,
$(a)
];
Let a = Date(a+1,'DD/MM/YYYY');
Next
Result
A calendar table with a single field is created. The field contains 365 rows, one per each day of the year.