Saved Bookmarks
| 1. |
Solve : endless loop? |
|
Answer» how to create an ENDLESS loop @echo offeven the variable j does not seem to be incrementing @echo off set j=2 for /L %%i IN ( 1,1,%j% ) DO ( echo %%i %j% set /A j = !j!+1 ) Try replacing the % with ! where noted.I think there is a BIT of confusion here... 1. If you want to CHANGE a variable in a loop, yes, you have to use exclamation marks ("exclamation points" in some places) but first you have switch on delayed expansion with this line setlocal enabledelayed expansion 2. In any case both of you have misunderstood an important point. At run time, when cmd.exe processes the FOR line, it expands the %j% variable into its value at that time, that is, the line is expanded to for /L %%i IN ( 1,1,2 ) DO etc and the value 2 is STORED in memory as the loop count limit. Whatever you do later to the variable j in the loop will not affect this. The FOR command does not check the value of %j% (or even !j!) each time round the loop. Therefore the loop will run exactly 2 times. %%A will successively store 1 and then 2 and then the loop will finish. %j% will always be 2. In this code... @echo off set j=2 for /L %%i IN ( 1,1,%j% ) DO ( echo %%i %j% set /A j = !j!+1 ) ...the variable %j% uses percent signs, there is no delayed expansion, the set /a command has no effect, and therefore %j% stays at 2 in this code... @echo off setlocal enabledelayedexpansion set j=2 for /L %%i IN ( 1,1,%j% ) DO ( echo %%i !j! set /A j = !j!+1 ) ...the variable !j! is an exclamation-mark variable, delayed expansion is enabled, so that the set /a command is able to add 1, so the first time around the loop, !j! equals 2, and the second time around the loop, !j! equals 3, and as I showed above, FOR does not care what you do to !j! in the loop. If you want an infinite loop, you can use a label and GOTO LIKE this... set j=2 :loop echo j=%j% set /a j=%j%+1 goto loop Terminate with CTRL-C Quote from: Bukhari1986 on May 23, 2010, 03:42:01 AM how to create an endless loop C:\test>type buk3.bat Code: [Select]@echo off setlocal enabledelayedexpansion :begin set /a j=2 for /L %%i IN ( 1,1,%1 ) DO ( echo i=%%i set /A j=!j! + 1 echo j=!j! ) goto begin Output: C:\test>buk3.bat 10 i=1 j=3 i=2 j=4 i=3 j=5 i=4 j=6 i=5 j=7 i=6 j=8 i=7 j=9 i=8 j=10 i=9 j=11 i=10 j=12 i=1 j=3 i=2 j=4 i=3 j=5 i=4 j=6 i=5 j=7 i=6 j=8 i=7 j=9 i=8 j=10 i=9 j=11 i=10 j=12 i=1 j=3 ^Ci=2 j=4 Terminate batch job (Y/N)? y C:\test> Thanks Salmon Trout Thanks marvinengland THANKS A LOT . |
|