For Statement List Implementations
The "FOR" construct offers looping capabilities for batch files. Following is the common construct of the ‘for’ statement for working with a list of values.
Syntax
FOR %%variable IN list DO do_something
The classic ‘for’ statement consists of the following parts −
Variable declaration – This step is executed only once for the entire loop and used to declare any variables which will be used within the loop. In Batch Script, the variable declaration is done with the %% at the beginning of the variable name.
List – This will be the list of values for which the ‘for’ statement should be executed.
The do_something code block is what needs to be executed for each iteration for the list of values.
The following diagram shows the diagrammatic explanation of this loop.
Following is an example of how the ‘goto’ statement can be used.
Example
@echo off FOR %%F IN (1 2 3 4 5) DO echo %%F
The key thing to note about the above program is −
The variable declaration is done with the %% sign at the beginning of the variable name.
The list of values is defined after the IN clause.
The do_something code is defined after the echo command. Thus for each value in the list, the echo command will be executed.
Output
The above program produces the following output.
1 2 3 4 5