Break Loop infinite
Moderator: DosItHelp
-
- Posts: 8
- Joined: 13 Nov 2012 20:27
Break Loop infinite
Hi All,
I have a Loop Infinite.
for /L %%n in (1,0,10) do (
echo "infinite\n"
if exist "%~dp0\Parts\file.xml"
(
//I want break here
)
)
anybody give me syntax break in batch file.
I have a Loop Infinite.
for /L %%n in (1,0,10) do (
echo "infinite\n"
if exist "%~dp0\Parts\file.xml"
(
//I want break here
)
)
anybody give me syntax break in batch file.
Re: Break Loop infinite
What do you mean break? Control C?
Explain what you are trying to do...
Explain what you are trying to do...
-
- Posts: 8
- Joined: 13 Nov 2012 20:27
Re: Break Loop infinite
I use batch file no use console.
Thanks.
Thanks.
Re: Break Loop infinite
foxidrive wrote:Explain what you are trying to do...
See above.
For example you can put in a statement and check for the existence of another file, and branch out of the loop.
It depends on what you want to do.
Re: Break Loop infinite
We had a thread a few months back about breaking out of FOR /L loops. Gotta dig that up.
Re: Break Loop infinite
This question was extensively discussed at infinite loop with break condition topic, and the last solution is here
Antonio
Antonio
Re: Break Loop infinite
To break in for-loop, use "goto ~ label" statement as followings.
for /L %%n in (1,0,10) do (
echo "infinite\n"
if exist "%~dp0\Parts\file.xml" (
goto :break
)
)
:: label
:break
for /L %%n in (1,0,10) do (
echo "infinite\n"
if exist "%~dp0\Parts\file.xml" (
goto :break
)
)
:: label
:break
Re: Break Loop infinite
@tomato: Please, test the previous program by yourself: it never ends and requires a Ctrl-C cancellation!
(perhaps you should read the referred topic so you know what we are talking about)
Antonio
(perhaps you should read the referred topic so you know what we are talking about)
Antonio
Re: Break Loop infinite
@Antonio
sorry for my mistake. I did not watch the code carefully.
@ngusaomanoi
In your for-loop,
(1, 0, 10) -> (start, step, end)
because the step is 0, the start(=1) cannot reach the end(=10).
Therefore it causes an infinite loop.
Replace step to any other value greater than 0.
eg. (1, 1, 10)

sorry for my mistake. I did not watch the code carefully.
@ngusaomanoi
In your for-loop,
(1, 0, 10) -> (start, step, end)
because the step is 0, the start(=1) cannot reach the end(=10).
Therefore it causes an infinite loop.
Replace step to any other value greater than 0.
eg. (1, 1, 10)

Re: Break Loop infinite
tomato wrote:(1, 0, 10) -> (start, step, end)
because the step is 0, the start(=1) cannot reach the end(=10).
Therefore it causes an infinite loop.
Right. That's probably why the title of this thread is "Break Loop infinite"

FWIW "goto :break" doesn't really work for finite loops, either, even though it may appear to. What happens is that the body of the for loop stops being executed after the goto, but the loop itself unrolls all the way to the end before jumping to the label. For a simple example, save the following as for123.cmd
Code: Select all
@for /l %%i in (1,1,3) do (
echo %%i
goto :break
)
:break
@echo done
Code: Select all
C:\tmp>for123
C:\tmp>(
echo 1
goto :break
)
1
C:\tmp>(
echo 2
goto :break
)
C:\tmp>(
echo 3
goto :break
)
done
C:\tmp>
Liviu