The goto statement in CMD interrupts the For Loop feature

Source: Internet
Author: User

In this program because of the use of the string cut in the last article, and the use of the Goto force Jump statement

However, when used in the program, but found an error, when the character cut the code snippet if directly as a non-nested statement to perform normal

But once placed in the compound statement of the For loop, it is found that the For loop executes only once and the For loop exits, and the value of the temporary variable%%x becomes empty.

In other words, goto jump and target: Labe cannot appear in a for statement at the same time

For example, the following example:

 for  Do (  : Show  echopause  goto: Show)

Looking directly at the code means it's easy to show the ABC string all the time, but the actual execution results are:

Only the first loop correctly executes the results we want, showing the ABC, but all behind is showing%i

In other words, when Goto jumps,%i loses the function of the temporary traversal variable of the original for loop, and becomes a normal string

As to why this is so I understand:

Because the GOTO statement is unconditionally forced jump, and ignores the program flow, that is, regardless of whether the target label is a compound statement will be forced to follow the label down, perhaps the label is the main process statement, perhaps the other if the compound statement block, perhaps the same as the Goto is a compound statement block, If you are jumping to the outside or other statement block, the current for loop must be exited.

But Cmd is the side of the interpretation side execution, he has no way and no obligation to check where the goto tag is located, theoretically, if jump to the main process is not a problem, if jump to another compound statement block in the affirmative target statement block temporary variable (%i) is certainly invalid, Because you do not know where to jump directly from, there is no main loop to execute the statement, how to talk about temporary variables, temporary variables are given by the main loop statement.

Only goto and label belong to a block of statements when the temporary variable should be effective. But as an interpreter for the side interpretation, there is no way to predict the contents of the next statement that is not entered at all.

So (conclusion):

Once a goto is encountered in the For statement, Cmd thinks this is a break, and no matter where you jump, the current for loop is definitely stopped, and the temporary variable (%i) given by the for loop will be invalidated. In the example above, although the jump is completed or in the For statement, but CMD has assumed that the for loop has exited, this is a standalone statement, not a compound statement

Workaround:

It is normal for the block of statements to be goto to be written into a function, written as a separate module, and then called in for.

The above example changes:

 for  Do (Call  : Show  %%i): Showecho%1pauseGoto  : Show

result, as we wanted, to go on displaying the ABC string

--Originally published in 2012-3-11 08:57

The goto statement in CMD interrupts the For Loop feature

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.