GOTO: Directs the Windows command interpreter to a labeled line in a batch program.


... The examples for the command "GOTO"
... "GOTO" Excerpt from Microsoft Windows Help
... Important information, tips for the "GOTO" command

The command: "GOTO" is on Windows 11, 10, .. available

The examples for the command "GOTO"

The `GOTO` command in Windows Command Prompt is used to control the execution of a batch script to a specific line number in the script. Here are some examples: Example 1: Simple GOTO:

@ECHO OFF
ECHO This is line 1
GOTO :Example
ECHO This will be skipped
:Example
ECHO This is the line after the GOTO

Description: The script is executed, and when it comes to the line with `GOTO :example`, it jumps to the label `:example` and execution continues from there. Example 2: Using labels for jumps:

@ECHO OFF
ECHO This is line 1
GOTO :Step1
ECHO This will be skipped
:Step 1
ECHO This is line 2
GOTO :Step2
:Step 2
ECHO This is line 3

Description: The script jumps from `:Step1` to `:Step2` and execution continues. Example 3: Conditional jumps with IF:

@ECHO OFF
SET username=John
IF "%Username%"=="John" GOTO :Success
ECHO This will be skipped
GOTO :End
:Success
ECHO Welcome, John!
:End

Description: This checks whether the username is "John". If yes, it jumps to `:success` and the corresponding message is printed. Otherwise it jumps to `:End`. Example 4: Looping with GOTO:

@ECHO OFF
SET /A counter=1
:LoopStart
ECHO loop iteration %Counter%
SET /A counter+=1
IF %Counter% LEQ 5 GOTO :LoopStart
ECHO loop ended

Description: Here a simple loop is created using GOTO. The loop is repeated five times before ending. Example 5: Error handling with GOTO:

@ECHO OFF
REM Here comes some code that could potentially cause an error

IF ERRORLEVEL 1 GOTO :Error handling
ECHO The code executed successfully
GOTO :End

:Error handling
ECHO An error has occurred. Error code: %ERRORLEVEL%
GOTO :End

:End

Description: Simple error handling is performed here using `GOTO`. If an error occurs (by checking the error level with `ERRORLEVEL`), it jumps to `:error handling` and an appropriate message is printed. It is important to note that excessive use of `GOTO` in scripts can result in code that is difficult to read and maintain. In many cases, other constructs such as loops and conditional statements (like `IF`) are preferred.

"GOTO" Excerpt from Microsoft Windows Help

Microsoft Windows [Version 10.0.19045.3693]
(c) Copyright 1985-2023 Microsoft Corp.

C:\\WINDOWS>

Directs cmd.exe to a labeled line in a batch program.

GOTO label

  label   Specifies a text string used in the batch program as a label.

You type a label on a line by itself, beginning with a colon.

If Command Extensions are enabled GOTO changes as follows:

GOTO command now accepts a target label of :EOF which transfers control
to the end of the current batch script file.  This is an easy way to
exit a batch script file without defining a label.  Type CALL /?  for a
description of extensions to the CALL command that make this feature
useful.

Important information, tips for the "GOTO" command

When using the `GOTO` command in the Windows Command Prompt, there are a few points to keep in mind to ensure that your batch script works properly and is easy to understand: 1. Readability and Maintainability: Excessive use of `GOTO` can result in confusing and difficult to understand code. It is often advisable to use alternative constructs such as loops (`FOR`, `WHILE`) and conditional statements (`IF`) to improve readability and maintainability of the script. 2. Name labels clearly: Labels (the places to jump to) should have clear and meaningful names to improve code understandability. For example, `:Start`, `:Step1`, `:End` are better than general names like `:Label1`, `:Label2`. 3. Structured programming: Structured programming promotes clear structures in the code. Use `GOTO` carefully and try to organize the code into logical blocks. 4. Avoid endless loops: If you use `GOTO` in conjunction with loops, make sure there is a mechanism that terminates the loop to avoid endless execution. 5. Ensure Labels Exist: Before jumping to a label, make sure the label actually exists in the script. Otherwise the script will exit with an error. 6. Error Handling: If you use `GOTO` for error handling, make sure you review the error code carefully and respond to it appropriately. Check errors with `ERRORLEVEL`.

    IF ERRORLEVEL 1 GOTO :Error handling
    
7. Prefer conditional statements: In some cases, conditional statements (`IF`, `ELSE`) can be a better alternative to `GOTO`, especially if it makes the structure of the script clearer. 8. Structured programming: Structured programming promotes clear structures in the code. Use `GOTO` carefully and try to organize the code into logical blocks. 9. Check alternatives: In some cases there may be more elegant alternatives to `GOTO`. Consider whether other constructs such as functions, loops, or conditional statements are more appropriate. 10. Add comments: When using `GOTO`, add comments to make the intent behind the jump clear. Particularly comment on complex or important jumps. It is important to note that modern programming practices often aim to minimize the use of `GOTO` and promote structured programming. In many cases, alternatives can make code more readable and easier to maintain.


Deutsch
English
Español
Français
Italiano
日本語 (Nihongo)
한국어 (Hangugeo)
汉语 (Hànyǔ)
Türkçe
Português
Português
Svenska
Norsk
Dansk
Suomi
Nederlands
Polski









Windows-10


... Windows 10 FAQ
... Windows 10 How To


Windows 10 How To


... Windows 11 How To
... Windows 10 FAQ



The command GOTO - Directs the Windows command interpreter to a labeled line in a batch program.

HTTP: ... console/en/035.htm
0.14
18513

Can I encrypt certain folders / files in Windows 11?

Does Windows 11 have disk cleanup for hard drives?

Where are the system fonts in Windows 11?

How do I schedule a restart for Windows Update on Windows 11?

Deactivate the window shadows under Windows 11, is that possible?

How can I minimize the other programs to tray, on Windows 11, 10, ...?



(0)