What Are Loops?
Loops let you repeat actions. Batch has two main loop types:
- for — repeat for items, numbers, files, etc.
- goto — manual loops using labels
Simple FOR Loop
for %%i in (1 2 3 4 5) do (
echo Number: %%i
)
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Looping Through Files
for %%f in (*.txt) do (
echo Found file: %%f
)
Counting with FOR /L
Format:
for /l %%i in (start,step,end) do ( ... )
Example:
for /l %%i in (1,1,10) do echo %%i
Goto Loop
@echo off
set count=1
:loop
echo %count%
set /a count+=1
if %count% LEQ 5 goto loop
echo Done!
Conditions (IF)
Batch IF statements check text, numbers, and file existence.
String Compare
if "%user%"=="admin" echo Welcome admin
Check If File Exists
if exist "data.txt" echo File found!
Number Compare
set age=18
if %age% GEQ 18 echo You are an adult.
Combining IF with SET /P
set /p choice=Enter Y/N:
if /i "%choice%"=="y" echo You chose YES.
if /i "%choice%"=="n" echo You chose NO.
Practical Example — Menu Loop
@echo off
:menu
echo === MENU ===
echo 1) Say hi
echo 2) Show date
echo 3) Exit
set /p option=Choose:
if "%option%"=="1" echo Hello!
if "%option%"=="2" echo Today is %date%
if "%option%"=="3" exit
goto menu
What’s Next?
Next you’ll learn about Python Data Types — the foundation of storing and working with information.