Skip to main content

Batch Script Break in Loops

Break in Loops

The break statement is used to alter the control flow within loops in any programming language.

The break statement is normally used in looping constructs and is used to cause immediate termination of the innermost loop.

note

The Batch Script language does not have a direct for statement that executes an interrupt, but this can be implemented using labels.

note

The break is implemented using two nested if: the second if condition is used to control when the break is implemented.

Example

In the following example, when the value of index reaches 2, we want to skip the statement which echoes its value to the command prompt and directly just increment the value of index.

@echo off 
set /A "index=1"
set /A "count=5"
:while
if %index% leq %count% (
if %index%==2 goto :Increment
echo The value of index is %index%
:Increment
set /A "index=index + 1"
goto :while
)
The value of index is 1
The value of index is 3
The value of index is 4
The value of index is 5

Table of Contents