Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
bhorneber
Luminary Alumni
Luminary Alumni

while $(a) > 0 vs. while a > 0

Hi,

I am hoping someone can explain the following behavior:

When executing this code in the load script, it will result in a potentially infinite loop. Main thing, it doesn't stop when a = 0 but continues to count down in the negative numbers:

Let a = 10;

Do while $(a) > 0

  TRACE $(a);

  Let a = a - 1;

Loop

By just changing the line  Do while $(a) > 0 to Do while a > 0 it works correctly.

Does someone have the explanation for why referencing the variable without $( ) works but with it doesn't? Really would like to understand this.

1 Solution

Accepted Solutions
jonathandienst
Partner - Champion III
Partner - Champion III

Because the $() expands to

     Do While 10 > 0

and the while loop is not executed again (trace step by step to see what i mean). So the expansion is not evaluated again and the condition is always true.

Logic will get you from a to b. Imagination will take you everywhere. - A Einstein

View solution in original post

5 Replies
alexandros17
Partner - Champion III
Partner - Champion III

You must write:

Let a = 10;

Do while $(a) > 0

  TRACE $(a);

  Let a = $(a) - 1;

Loop

in this way you are correctly decreasing the value of a

jonathandienst
Partner - Champion III
Partner - Champion III

Because the $() expands to

     Do While 10 > 0

and the while loop is not executed again (trace step by step to see what i mean). So the expansion is not evaluated again and the condition is always true.

Logic will get you from a to b. Imagination will take you everywhere. - A Einstein
Peter_Cammaert
Partner - Champion III
Partner - Champion III

The help text says the same (although in something that looks like fine print): "... each condition is interpreted only the first time it is encountered but is evaluated for every time it (sic) encountered in the loop", meaning that the $-sign substitution is indeed only applied once by the parser (at the very start) and the logical expression is probably translated into some kind of intermediate code (performance, you know) so that it quickly evaluates to either true or false.

Weird.

Peter

[Edit] Eliminated irrelevant part of this comment

bhorneber
Luminary Alumni
Luminary Alumni
Author

Thanks Jonathan and Peter.

Clear and very helpful answers!

Not applicable

Hi benedikt,

"SET" means it takes the value after '=' operator as a string. to evaluate any operation like set a= 4+1 we use $(a).

You could not do operations like ' set a = $(a)+1;'

If you use the 'LET' then don't need $(variable).

in your case

set a = 10; (or) let a=10;

do while a>0

trace $(a);

let a = a-1;

loop;