I just don't seem to understand.
At the first loop, $ expansion will compute the expression and will find that vCompteur is equal 1. But in the loop the value of the variable vCompteur will change to 2 and then when $ expansion will compute the expression normally it should find that vCompteur is equal 2.
Something doesn't make sense, or maybe I'm missing something.
Actually your second script is actually the correct way to code this loop. Used this way vCompteur behaves like a variable in a normal language, and so does the do while loop
To explain why the first one does not work - which is that the variable expansion happens before the script line is interpreted, this creates a compare between two number literals, not a variable and a number. The while condition is set up from this (once), hence an infinite loop.
Welcome to fun world of QlikView scripting
I believe not all assumptions from common stock programming languages are working here.
To illustrate my point I slightly modified your script. It is still perfectly valid, and give correct output, though syntax highlighter not happy with lines 6-7 now.
///$tab Main LET vCompteur = 1; LET whileVar = 'vCompteur'; LET doWhile = 'DO while'; LET whileCondition = '<= 10'; $(doWhile) $(whileVar) $(whileCondition) TRACE $(vCompteur); LET vCompteur = $(vCompteur) + 1; LET whileVar = 'ABRAKADABRA'; LOOP LET vCompteur=; LET whileVar=; LET doWhile=; LET whileCondition=;
Best way to look at inner working of qlikview script engine is to step through script debugger.
Lets step through up to 6 line.
Before debugger step into command line each occurence of $(something) in that line is textually replaced by its value in key/value map in right-bottom box (dictionary/map for variables).
If we step through futher we can see that debugger never return to line 6. Loop doing its job between lines 7-10.
So our intentional distortion of variable whileVar do not break script either.
Firstly I intuitively think about $(someThing) as of evaluation of variable someThing. It's actually purely a string substitution (regex replace) on a stage before any evaluation.
I hope it would clarify this vague behaviour.