Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
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.
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.
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
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.
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
Thanks Jonathan and Peter.
Clear and very helpful answers!
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;