1

I'm trying to simulate a while loop in Batch, here's my code:

@echo off
set test=0

:main
call:whileLSS %test% 4 :count

:whileLSS
if %1 LSS %2 (
    echo %1
    call%3
    goto whileLSS
)
goto:EOF

:count
set /a test=%test%+1
goto:EOF

This just outputs 0s, instead of outputting "0 1 2 3" like I want it to.

The problem is that the loop goes forever because %1 doesn't have the most updated value of test.

Is this the right approach?

How do I update the value of %1?

Is there a way to not have to hardcode operators like LSS?

Nihil
  • 37
  • 1
  • 8
  • 3
    Give this a read. http://www.dostips.com/forum/viewtopic.php?t=3487 – Squashman Dec 01 '16 at 20:21
  • You can't update the value of a command line argument. You would have to assign it to an environmental variable first and then update it. – Squashman Dec 01 '16 at 20:22
  • Related: [while loop in batch](http://stackoverflow.com/questions/1788473/while-loop-in-batch) and [how to do a while loop on batch](http://stackoverflow.com/questions/36133092/how-to-do-a-while-loop-on-batch) – aschipfl Dec 01 '16 at 20:25

3 Answers3

3

as you've been told, you can't change an Arg, you can take the arg as a reference and change the referenced var, this requires delayed expansion here.
Your first sub also wasn't separated from flow.

This Batch:

@echo off&Setlocal EnableDelayedExpansion
set test=0

:main
call:whileLSS test 4 :count
Goto :Eof

:whileLSS
if !%1! LSS %2 (
    echo !%1!
    call%3
    goto whileLSS
)
goto:EOF

:count
set /a test+=1
goto:EOF

Produces this output:

0
1
2
3

Edit
The operator to the if may also be suppied as an arg:

@echo off&Setlocal EnableDelayedExpansion
set test=0

:main
call:while test LSS 4 :Increment
set test=10
call:while test GTR 4 :Decrement

Goto :Eof
:while
if !%1! %2 %3 (
    echo !%1!
    call %4 %1
    goto while
)
goto:EOF

:Increment
set /a %1+=1
goto:EOF

:Decrement
set /a %1-=1
goto:EOF
  • Is there a way to take another argument to use an operator? That way I don't have to make a while for LEQ, GTR and GEQ – Nihil Dec 01 '16 at 21:27
  • @Nihil yes, just tested it, see append to the answer. –  Dec 01 '16 at 21:44
  • @Nihil Did another edit to the subs, but I now feel a bit vrtualized ;-) And I personally prefer straight code I still understand a year later. –  Dec 01 '16 at 22:01
  • strange, for some reason when I tried having a fourth argument it always had the value of "4", now it works :) – Nihil Dec 02 '16 at 16:18
0

Like this may be?

@echo off

:main
set /a a=1
set /P i=Enter i:
call:whileLSS %a% %i%

:whileLSS
    echo %1
    if %1 LSS %2  call:reinitialize %1 %2
    goto:EOF


:reinitialize
    set /a c=%1
    set /a b=%c%+1
    set /a d=%2
    call:whileLSS %b% %d%

goto:EOF
  • even if this seems to answer the question, it would be best to provide explanations on your code and on the OP code (why it didn't work, why your code does what's required) – Jean-François Fabre Dec 01 '16 at 20:46
0

Try this:

@echo off 
Setlocal EnableDelayedExpansion
set test=0

:main
call :whileLSS !test! 4 
Goto :Eof

:whileLSS
set i=%1
set j=%2

:loop
if !i! LSS !j! (
    echo !i!
    call :count
    goto :loop
)

goto :EOF

:count
set /a i+=1
goto :EOF
Kirk Beard
  • 9,569
  • 12
  • 43
  • 47
Blake K Akiti
  • 173
  • 1
  • 3
  • 9