100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Batch

Goto and Labels

Understand how batch scripts use labels and the goto command to jump execution, build subroutines with call, and structure menu-driven scripts.

Variables and Control FlowIntermediate8 min readJul 10, 2026
Analogies

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.

batch
@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.

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 LABEL jumps to it unconditionally with no implicit return.
  • Labels are case-insensitive and local to the script file they appear in.
  • call :LABEL invokes a subroutine and returns control to the line after the call once exit /b runs, unlike plain goto.
  • Parameters passed via call :Sub %%VAR%% are read inside the subroutine as %1, %2, and so on.
  • Placing exit /b before 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

Was this page helpful?

Topics covered

#Batch#WindowsBatchScriptingStudyNotes#MicrosoftTechnologies#GotoAndLabels#Goto#Labels#Command#Building#StudyNotes#SkillVeris