Using Goto

Other topics

Remarks:

So in other words, if the number the player inserted is 1, it'll go back to the :Name part of the code.

so if the input is equal to 1, go back to the line with :Name

Make Sure if you use this, the word begins with the Colen (:).

Example Programs

For Example:

echo Hello!
pause >nul
:Name
echo What Is Your Name
set /p Input=Name: 
echo so %Input% Is Your Name, right?
echo Rename?
echo 1 For Yes
echo 2 For No
set /p Input=Rename:
if %Input%=1 goto Name

Another Example:

@echo off
echo 1 or 2?
set /p input=Choice: 
if %input%=1 goto Skip
echo You Chose 1
pause >nul
echo So time for stuff
pause >nul
echo Random Stuf
pause >nul
:Skip
echo So that's it.
pause >nul

Goto with variable

Goto accepts the use of variable value to act as the label to goto.

Example:

@echo off

echo a = 1
echo b = 2

set /p "foo=Enter option:"
goto %foo%

However, you should check the input so it will not go to somewhere that does not exist. Going to an undefined label will terminate your batch script instantly.

Syntax:

  • goto :Label
  • goto Label
  • goto :EOF

Parameters:

ParameterDetails
:LabelAny label that is valid (defined by :<LabelName>)
:EOFA pre-defined label that exits the current script of function(same as exit /b)

Contributors

Topic Id: 9164

Example Ids: 28459,28670

This site is not affiliated with any of the contributors.