Solved for /L, is this a bug, or am i buggy?

July 16, 2012 at 21:40:14
Specs: Windows Xp klunker, various (16+ computers)
Attempted to use "goto :" to eject from a "for /L" loop, but no output either way:

:usage: XX maxloopcount jumpoutvalue
echo %time%
for /L %%a in (1,1,%1) do (
echo %%a
if %%a equ %2 goto :xx
echo loop-exited
goto :eof

echo %time%
echo bye

note the difference in times using values (maxloopcount) 1000, 10000, 100000, but no output from the loop, and no loop-exit - it just goes into its own "zone": comatose but still breathing, regardless of the "jumpoutcount" value.. This drove me nuts thinking it was my code. Is it?

See More: for /L, is this a bug, or am i buggy?

Report •

July 17, 2012 at 03:05:50
✔ Best Answer
For /l loops don't actually break with goto, execution stops having effect but it always finishes counting. The flow of execution does jump to the correct point after the loop is finished. If the number is large this can take some time.

You can see the effect by running the script with echo turned on. The script should actually seem to work fine with a relatively low maxloopcount, try it with 10.

If it wasn't for this problem it would be easy to simulate a while loop with for /l using a zero increment( "(0,0,0) / (1,0,1) / ect" or "(0)" ). Since a zero increment actually creates and infinite loop it doesn't stop counting either(at least in tolerable limits).

Report •

July 17, 2012 at 06:43:30
Thanks,Judago. I learn a lot in your explanation. I'm new to batch. I found that,you may have already noticed, If maxloopcount is smaller than jumpoutvalue you can see the loop exit.
for example:
XXX.bat 1000 1005
Thanks for tolerating my awful english and stupid thoughts above.

Report •

Related Solutions

Ask Question