1.

Solve : stopping loops?

Answer»

does anyone know how to stop a goto LOOP,
im trying to right a program that will run a loop that runs from 1 to 100 by user entered variable, i know how to make the variable, i just dont know how to make a loop run so many times, then stop, so if some ONE could help me out that would be greatHi goodnatureddog,

The following code will loop 10 times, and has been tested on Windows XP [Version 5.1.2600] cmd.exe :-

Code: [Select]@echo off
set COUNTER=1

:startloop
echo Counting from %COUNTER% to 10 ...
echo (Doing something...)

if %COUNTER%==10 goto stoploop
set /A COUNTER=%COUNTER%+1
goto startloop

:stoploop
echo Finished!

The following code will prompt how many times to loop :-

Code: [Select]@echo off
set COUNTER=1

set /P STOPPER=Loop how many times?

:startloop
echo Counting from %COUNTER% to %STOPPER% ...
echo (Doing something...)

if %COUNTER%==%STOPPER% goto stoploop
set /A COUNTER=%COUNTER%+1
goto startloop

:stoploop
echo Finished!

Please NOTE the prompt is NOT validated. If user INPUTS an invalid integer (or zero or negative) then the code will loop forever.

Hope that helps?

Regards,
James

Better solution to loop 10 times, tested on Windows XP [Version 5.1.2600] cmd.exe :-

Code: [Select]@echo off

for /L %%C IN (1,1,10) DO (
echo Counting from %%C to 10 ...
echo [Doing something...]
)

echo Finished!

Better solution to prompt how many times to loop :-

Code: [Select]@echo off

set /P STOPPER=Loop how many times?

for /L %%C IN (1,1,%STOPPER%) DO (
echo Counting from %%C to %STOPPER% ...
echo [Doing something...]
)

echo Finished!

Hope that helps?
James
ok now i just have two more questions,
one- what does the /a switch do and
two- i need to say if the user entered variable is GREATER the 100, to reask the amount
other wise, my loop worked
thxCode: [Select]@echo off
set COUNT=0
:redo
cls
set /p MAX=How many numbers?
if %MAX% GTR 100 goto redo
cls

Add this part. If MAX is larger than 100, it clears the screen and asks for the number again.answering the question, "what does the /a switch do?", it tells the SET command that we are doing (a)rithmetic, not dealing with text strings.


Quote

@echo off
set /a SUM=5+2
echo %sum%
pause

Here is an example of the /a switch. It calculates '5+2' and echo's the answer.


Discussion

No Comment Found