Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Hi Experts,
I know how the for loop works. I use it to load multiple tables, to create all variables in QlikView present in excel file.
I wonder how the nested for loop works. I see the code looks like below-
for i =1 to 10
//code
for i =1 to 10
//code
next
next
I have seen various examples in the community, but find it difficult to understand the code.
-Which for loop runs first?
-How many max nestings we can do?
-Could anyone explain me simple examples or some use cases where we can use nested for loop?
Thanks
Could anyone explain me simple examples or some use cases where we can use nested for loop?
you can try to see the result adding a trace to the loop, example (nested loop to generate odd month start date
for year = 2015 to 2016
for month = 1 to 12 step 2
trace year=$(year) month=$(month);
😧
load makedate($(year), $(month)) as OddMonthStart
AutoGenerate 1;
next month
next year
Which for loop runs first?
in my example
year 2015
month 1 3 5 7 9 11
year 2016
month 1 3 5 7 9 11
How many max nestings we can do?
I don't know but I never used more than 2 nested loop
Could anyone explain me simple examples or some use cases where we can use nested for loop?
you can try to see the result adding a trace to the loop, example (nested loop to generate odd month start date
for year = 2015 to 2016
for month = 1 to 12 step 2
trace year=$(year) month=$(month);
😧
load makedate($(year), $(month)) as OddMonthStart
AutoGenerate 1;
next month
next year
Which for loop runs first?
in my example
year 2015
month 1 3 5 7 9 11
year 2016
month 1 3 5 7 9 11
How many max nestings we can do?
I don't know but I never used more than 2 nested loop
In your example you have two loops but both have the same index i.
You need two diferent index
for i =1 to 10
//code for i
for k =1 to 10
//code for k off each i
next
next
The sequence are:
i=1
//code for i=1
k=1
//code for k=1 and i=1
k=2
//code for k=2 and i=1
...
k=10
//code for k=10 and i=1
i=2
//code for i=2
k=1
//code for k=1 and i=2
k=2
//code for k=2 and i=2
...
k=10
//code for k=10 and i=2
...
...
Thanks for the replies.
I am now getting idea how it works.
Personally I have issues identifying the correct tool( say nested for loop) to fulfill a particular requirement.
Could you list out few common requirements which nested for loop can fulfill, which cannot be achieved by normal for loop?