Skip to content

Batch Script Break in Loops

Learn how to implement a break-like functionality in Windows Batch scripts using labels and nested IF conditions to exit loops early.

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.

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 
)
Outputplaintext
The value of index is 1
The value of index is 3
The value of index is 4
The value of index is 5
Navigation