CMD goto command process jump

Source: Internet
Author: User

If you need to execute different established operations for different situations, but you still need to follow the conventional execution process, the task cannot be completed. In this case, you need to introduce the concept of process redirection, use the process jump statement Goto. The default execution sequence is changed to force the jump to the specified position to execute a specific Program Block.

Let's look at an example: assume that you need to determine whether the user inputs A or B,CodeIt can be written as follows:Copy codeThe Code is as follows: @ echo off
Set/P input = enter the letter A or B:
If "% input %" = "A" goto
If "% input %" = "B" Goto B
Pause
Exit

:
Echo the letter you entered is
Pause
Exit

: B
Echo the letter you entered is B
Pause
Exit

According to the general execution process, all the code will be executed from top to bottom line by line, the result will show the two lines, "the letter you entered is a" and "the letter you entered is B". Obviously, this does not meet our requirements. However, the actual result is: if the letter you entered is not a, that is, B, the result will only show "the letter you entered is a" or "the letter you entered is B"; that is, the execution sequence of the Code has changed and is no longer invariably executed from top to bottom line by line: when the letter A is entered, the first if statement will be executed. After the statement is executed, it will jump to the label segment: a, which bypasses all the code lines between the first if statement and: A. When the letter B is entered, the first two if statements are executed. After the second if statement is executed, this will jump to the label segment: B, bypassing all the code lines between the second if statement and: B.

From the above example, we can see that to make the GOTO statement take effect, we also need the cooperation of A, B, and other label segments. The so-called label segment is such a code block: the first line is followed by a single colon, followed by a string of characters, the second line and all subsequent behavior code statement lines. Here, the colon in the first line is the label sign, and the string that follows it is the label name. The part after a GOTO statement is the tag name, which directs the execution of the statement to where to jump, the tag receives the jump command of the GOTO statement, and guides subsequent operations of the GOTO statement. The Goto statements and labels echo each other and complement each other in the process jump.

Note the following when using the jump statement Goto:

1. The GOTO statement and the tag must echo each other. The program will exit without finding the corresponding tag segment; only the tag segment and no GOTO statement are allowed, but the process jump function cannot be implemented. From the perspective of simplified code, if there is a tag segment and no GOTO statement, this tag is redundant, can be simplified;
2. If there are multiple tag segments, You Need To note whether appropriate exit statements need to be added between the tag segments to terminate the program and continue to execute downward. This is where many new users are prone to mistakes, be cautious. For example, if the exit statement between A and B is removed, the result will show both "the letter you entered is a" and "the letter you entered is B, the retain exit statement will only display "the letter you entered is a", and then press any key to launch the batch processing program, because the batch processing is executed from top to bottom row by row, that is, after you use the GOTO statement to jump to a new position, the execution process is maintained until another process jump statement is run. The execution process is not automatically terminated between two tag segments.
3. The label name can only use constants, but not variables;
4. If a tag segment with the same name exists, the tag segment at the top level will be executed, and whether the tag segment after it is executed will be processed as needed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.