Breaking Out of a For Loop

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Breaking Out of a For Loop

Post by MigrationUser »

23 Mar 2013 20:59
TimaBVI

Hi everyone,

Anyone ever wondered how to break out of a FOR loop? The only way I've found to
do this is to create a subroutine, call it, then place the loop in the subroutine. Whenever
you want to break the loop, use GOTO :eof .

Example:

The following code has a loop that is configured to run exactly 10 times but breaks after the 5th loop.

Code: Select all

@ECHO OFF

SETLOCAL ENABLEDELAYEDEXPANSION

CALL :MySubroutine
PAUSE

:MySubroutine
FOR /L %%A IN (1,1,10) DO (
ECHO %%A
IF %%A==5 GOTO :eof)

ENDLOCAL

Result:

1
2
3
4
5
----------------------------

#2 01 Apr 2013 03:34
mkwayisi


Erhm, I'm not sure but what is wrong with doing it this way:

Code: Select all

@ECHO OFF

FOR /L %%A IN (1, 1, 10) DO ECHO %%A & IF %%A == 5 GOTO BREAK
:BREAK

PAUSE
----------------------------

#3 03 Apr 2013 16:06
Squashman


We had a whole thread dedicated to this on DosTips.
dostips.com/forum/viewtopic.php?f=3&t=2707

----------------------------

#4 04 Apr 2013 02:30
Ocalabob


@Squashman
Great to see you on this forum! What took so long? :)

Best wishes!

----------------------------

#5 05 Apr 2013 16:07
TimaBVI

Hi everyone,

Nothing wrong with that way.
Either way is fine.

Thanks for the reply.

Last edited by TimaBVI (05 Apr 2013 16:11)

----------------------------

#6 05 Apr 2013 17:41
foxidrive


The upshot is that doing it using methods above will work functionally, but the rest of the file is actually processed. If you have another 20,000 lines in the file then it will take some time to finish.

Dostips showed some methods that quit immediately.

----------------------------

#7 06 Apr 2013 09:53
foxidrive


I wasn't too clear, but try this to see the effect. It stops after 5 echos but see what else happens...

Code: Select all

@ECHO OFF

CALL :MySubroutine
PAUSE
GOTO :EOF

:MySubroutine
FOR /L %%A IN (1,1,10000000) DO (
ECHO %%A
IF %%A==5 GOTO :eof)
----------------------------

#8 06 Apr 2013 09:56
foxidrive


Similarly with this code:

Code: Select all

 @ECHO OFF

FOR /L %%A IN (1, 1, 10000000) DO ECHO %%A & IF %%A == 5 GOTO BREAK
:BREAK

PAUSE  
----------------------------

#9 06 Apr 2013 22:01
TimaBVI


Hi everyone,

I increased the last number in the FOR loop to 100000000 and it displayed the first five numbers and kept running
for some seconds.

I did not develop the cmd so here is my opinion on why that happens.

It's probably how the command interpreter processes certain commands. For the FOR command, the interpreter first
reads the line and calculates how many times it needs to run. Second, whatever statements are in the FOR command
is processed separately from the FOR command itself. When the last statement is reached, the part responsible for
processing the statements, returns an errorlevel to the part responsible for processing the loop. That returned number
determines whether the interpreter should continue processing statements and the loop continues.

So when the GOTO statement is reached, an errorlevel is returned that means cease all further processing of statements
within the FOR command and then the loop continues.

Make sense?

----------------------------

#10 30 Apr 2013 04:45
Aacini


An explanation of this problem, and the way to solve it, is in this DosTips topic:

http://www.dostips.com/forum/viewtopic.php?f=3&t=3487

The solution may be used to assemble a completely functional While loop.

----------------------------

#11 30 Apr 2013 12:14
TimaBVI


Hi Aacini,

After reading the topic at dostips, placing the loop in a separate cmd context
seems sensible if you are aiming for better performance. I will try it some time.
Post Reply