首页 > 解决方案 > 如何“打破” For 循环?

问题描述

下面,当我执行goto命令时,它只是挂起,我必须控制-C。我也试过EXIT /b。我试图尽可能地避免 goto。有没有办法做我想做的事?

:SUB_bigRandLooper

set /a lcv=0

FOR /L %%s IN ( 0 , 0 , 1 ) DO (

    set big-rand=!random:~-4!
    echo big-rand is !big-rand!
    set /a lcv=%lcv+1
    if !big-rand! GTR 9900 goto bigRandLooperWrapup
)

:bigRandLooperWrapup

echo biggest-rand is %big-rand%
echo lcv is %lcv%

EXIT /B

.

标签: windowsbatch-filecmd

解决方案


简短的回答是:不,你不能。


由于您正在使用for /L并建立一个无限循环,并且循环在执行之前已被预处理和缓存,因此它不能被goto;中断。goto打破循环上下文,或者更准确地说,(/)块上下文,因此块中不再执行命令,但循环本身仍在运行。

您可以通过以下代码证明这一点:

for /L %%I in (1,1,100000) do (
    echo %%I
    if %%I equ 10 goto :SKIP
)
:SKIP
echo skipped

你会看到echo %%I它只对%%Ifrom 1to10执行,但执行不会立即继续,echo skipped但有一个明显的延迟,因为循环在后台完成迭代,尽管没有执行更多命令。


不过有一个解决方法:您可以使用 建立一个无限循环goto,如下所示:

:SUB_bigRandLooper

set /A lcv=0
:bigRangLooperLoop
set big-rand=!random:~-4!
echo big-rand is !big-rand!
set /A lcv+=1
if !big-rand! gtr 9900 goto :bigRandLooperWrapup
goto :bigRangLooperLoop

:bigRandLooperWrapup
echo biggest-rand is %big-rand%
echo lcv is %lcv%
exit /B

我知道goto循环比循环慢for /L,但这是创建可破坏无限循环的唯一方法。

一种更快的方法是嵌套两个循环方法:使用for /L迭代几千次并环绕一个无限goto循环。


另一种解决方法是利用exit命令可以中断(无限)for /L循环的事实。但是由于这也会退出cmd运行批处理文件的实例,因此需要将循环放入单独的cmd实例中。当然,环境与当前环境完全分离。解决方案可能如下所示:

:SUB_bigRandLooper
@echo off
rem // Check for argument, interpret it as jump label if given:
if not "%~1"=="" goto %~1

rem /* Establish explicit `cmd` instance for executing the `for /L` loop;
rem    the `for /F` loop implicitly creates the new `cmd` instance for the command
rem    it executes, so we do not have to explicitly call `cmd /C` here; the resulting
rem    values are echoed by the sub-routine and captured here by `for /F`: */
for /F "tokens=1,2" %%I in ('"%~f0" :bigRandLooperLoop') do (
    rem // Assign captured values to environment variables:
    set "big-rand=%%I" & set "lcv=%%J"
)

:bigRandLooperWrapup
echo biggest-rand is %big-rand%
echo lcv is %lcv%
exit /B

:bigRandLooperLoop
setlocal EnableDelayedExpansion
set /A lcv=0
for /L %%s in (0,0,1) do (
    set big-rand=!random:~-4!
    rem /* Explicitly redirect this output to the console window to prevent it from
    rem    being captured by `for /F` in the main routine too: */
    > con echo big-rand is !big-rand!
    set /A lcv+=1
    if !big-rand! gtr 9900 (
        rem // Output found values in order to be able to capture them by `for /F`:
        echo !big-rand! !lcv!
        rem // Break loop and leave current `cmd` instance:
        exit
    )
)
endlocal

推荐阅读