Skip to main content

Batch Script For Loop in Ranges

For Loop in Ranges

for loop can also be implemented through a range of values.

Syntax

The following is the common syntax of the for statement for working with a list of values.

for /l %%var_name in (Lowerlimit, Increment, Upperlimit) do some_code

where:

  • /l signifies that for loop is used for iterating through a range of values
  • Lower limit is the value from which loop will start until it reaches the Upper limit and the increment is the value with which lower limit will be increased during each iteration.
  • The some_code code block is what will be executed for each iteration

Example

For example, this script will examine the range from 0 to 3, incrementing the value by 1 with each iteration.

@echo OFF
for /l %%y in (0, 1, 3) do echo %%y
1
2
3