Labels and the Goto Command
A label in a batch file is a line starting with a colon, like :START, and marks a jump target; goto START transfers execution immediately to that line, skipping everything in between. Unlike structured loops, goto performs an unconditional jump with no implicit return, so batch files often combine goto with if to build conditional loops, such as a retry loop that jumps back to :START until a condition is met. Labels are local to the file they appear in and are not case-sensitive, so :End and goto end refer to the same location.
Cricket analogy: Umpires calling for an immediate over-the-wicket re-set after a no-ball is like goto REBOWL, an unconditional jump straight back to a specific point in the sequence of play, skipping everything else.
@echo off
:START
set /p INPUT=Enter a number 1-10:
if %INPUT% LSS 1 goto START
if %INPUT% GTR 10 goto START
echo Valid number: %INPUT%
goto END
:END
echo Done.
Building Subroutines with Call
call :LABEL invokes a labeled section as a subroutine and returns to the line after the call once exit /b is reached, unlike plain goto, which never returns. This lets a script reuse a block of logic from multiple places without duplicating code, similar to a function in other languages, and parameters can be passed as call :Greet %%NAME%%, read inside the subroutine as %1. Placing exit /b at the very top of the script, before any labels, is essential when subroutines are defined below the main logic, otherwise execution would fall through into the first label's code unintentionally.
Cricket analogy: Calling in a specialist fielding coach for a ten-minute catching drill and then having play resume exactly where it left off is like call :FieldingDrill, a reusable subroutine that returns control afterward, unlike a one-way goto.
Parameters passed to a subroutine via call :Greet %%NAME%% are read inside the subroutine as %1, %2, and so on, exactly like command-line arguments passed to the whole script.
Menu-Driven Scripts with Goto
A common real-world pattern combines choice or set /p with goto to build a text menu: the script displays options, reads the user's selection, and uses if statements to goto the matching labeled section, looping back to the menu with goto MENU after each action completes. This structure avoids needing actual loop constructs for indefinite repetition, since goto combined with a label placed above the input prompt naturally creates a repeat-until-exit loop. Care must be taken to include an explicit exit path (an exit or goto END on a quit option), or the menu will loop forever with no way out.
Cricket analogy: A scoring app showing a menu of Bat, Bowl, Field, Exit and jumping to the matching section based on the operator's choice is like a batch menu using goto MENU to loop back after each action.
A menu built with goto MENU but no exit branch (missing an exit or goto END on the quit option) will loop forever, since goto never terminates the script on its own -- always give the user an explicit way out.
- A label is a line starting with a colon, like
:START;goto LABELjumps to it unconditionally with no implicit return. - Labels are case-insensitive and local to the script file they appear in.
call :LABELinvokes a subroutine and returns control to the line after the call onceexit /bruns, unlike plain goto.- Parameters passed via
call :Sub %%VAR%%are read inside the subroutine as %1, %2, and so on. - Placing
exit /bbefore any labels defined at the bottom of a script prevents execution from falling through into subroutine code unintentionally. - Combining goto with if creates conditional loops, such as retry-until-valid input patterns.
- Menu-driven scripts loop back to a menu label with
goto MENU, but must include an explicit exit path or they never terminate.
Practice what you learned
1. What is the key difference between `goto :Sub` and `call :Sub`?
2. How are parameters passed to a subroutine via `call :Greet %%NAME%%` accessed inside the subroutine?
3. Why is it important to place `exit /b` before any labels defined at the bottom of a script?
4. Are batch labels case-sensitive?
5. What happens if a goto-based menu loop has no exit branch?
Was this page helpful?
You May Also Like
If/Else in Batch
Master conditional logic in Windows batch files: comparing strings and numbers, testing file existence, checking errorlevel, and chaining else branches.
The Choice Command
Use the built-in choice command to build single-keypress menus, timed prompts, and errorlevel-driven branching in Windows batch scripts.
Variables in Batch Scripts
Learn how Windows batch scripts store, read, and manipulate values using the set command, environment variables, delayed expansion, and substring syntax.
Related Reading
Related Study Notes in Microsoft Technologies
Browse all study notesWindows 10 / UWP Development Study Notes
.NET · 30 topics
Microsoft TechnologiesMFC (Microsoft Foundation Classes) Study Notes
C++ · 30 topics
Microsoft TechnologiesSilverlight Study Notes
.NET · 30 topics
Microsoft TechnologiesXAML Study Notes
.NET · 30 topics
Microsoft TechnologiesWPF (Windows Presentation Foundation) Study Notes
.NET · 30 topics
Microsoft TechnologiesMVVM Design Pattern Study Notes
.NET · 30 topics