DOS Process Jump Goto_dos/bat

Source: Internet
Author: User
Tags goto
If for different situations, need to perform different established operations, if or according to the normal execution process, is unable to complete the task, this time, it is necessary to introduce the concept of process jump, using the process jump statement goto. The meaning of a process jump is to change the default order of execution, forcing the jump to the specified location to execute a particular block of the program.

Let's take a look at an example: suppose you need to determine whether a user is typing a or b, and the code can be written like this:
@echo off
set/p input= Please enter the letter A or B:
If "%input%" = = "a" goto a
If "%input%" = = "B" Goto b
Pause
Exit

: A
Echo, the letter you typed is a.
Pause
Exit

: B
Echo, the letter you typed is B.
Pause
Exit

According to the general execution process, all the code is executed from top to bottom, and the result will show "the letter you entered is a" and "the letter you entered is B". Obviously, this is not up to our requirements; however, the actual result is that if you enter a letter that is not a B, then the result will only show " The letter you entered is "a" or "the letter you entered is B"; that is, the sequence of execution of the code has changed, and it is no longer being executed from top to bottom in the same way: When the letter A is entered, the first if statement is executed, and after this statement is executed, it jumps to the label segment: A, bypassing the first if statement and: All lines of code between a, when the letter B is entered, the first two if statement is executed, and when the second if statement is executed, it jumps to the label segment: b, bypassing all lines of code between the second if statement and: B.

As you can see from the above example, to make a goto statement work, you also need to: match the label segments such as A or: B. The so-called label segment is such a code block that starts with a single colon on the first line, followed by a string of characters, and then all the behavior code statement lines followed by the second line. Here, the first line of the colon is the label flag, followed by the string is the label name, both collectively labeled. The part following the GOTO statement is the label signature, which guides the execution of the statement after it has been executed, and the label receives the jump instruction of the Goto statement and directs the subsequent operation of the Goto statement. Goto statements and tags echo each other, in the implementation of the process jump, complementary, indispensable.

Here's a note about using the Jump statement goto:

1, goto statements and tags to each other, not only goto statements and no corresponding label segments, otherwise, the program will not find the corresponding label segment and directly exit; only the label segment and no goto statement is allowed, but not the process jump function, from the point of view of the code simplification, There is a label segment without goto statement, then this label is superfluous, can be streamlined;
2, if there are multiple label segments, you need to pay attention to whether the label section needs to add appropriate exit statements to terminate the program to continue to execute downward, this is a lot of beginners are easy to make mistakes in the place, need to be cautious. Take the above code for example, if you remove: A and: The exit statement between B, then, when the letter A is entered, the result will also display "The letter you entered is a" and "the letter you entered is B", and the left exit statement will only appear after the word "you entered is a" Press any key to eject the batch program, because the batch process is executed from top to bottom, that is, after the goto statement is used, the execution flow remains in the new position until another process jump statement is encountered, and the execution does not automatically terminate between the two label segments.
3, the label name can only use constants, and can not use variables;
4. If there is a label paragraph with the same name, the label section at the top level will be executed and the subsequent label paragraph will be implemented, depending on the specific circumstances.

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.