Go language flow control's goto statement and Infinite loop _golang

Source: Internet
Author: User
Tags goto

Goto statement

The goto statement in the Go programming language provides the ability to jump from jump to tag declaration unconditionally.

NOTE: Using GOTO statements is highly discouraged in any programming language because it makes it difficult to track the program's control process, making the program difficult to understand and difficult to modify. Using a goto any program can be rewritten so that it does not need goto.

Grammar
The syntax to go to the Goto statement is as follows:

Copy Code code as follows:

Goto label;
..
.
Label:statement;


Here, the label (label) can be any plain text that is dropped from the keyword, and it can be set anywhere above or below the GO program to use the goto statement.

Flow chart:

Example:

Copy Code code as follows:

Package Main

Import "FMT"

Func Main () {
/* local variable definition * *
var a int = 10

* Do loop Execution/*
Loop:for a < 20 {
If a = = 15 {
/* Skip the iteration * *
A = a + 1
Goto LOOP
}
Fmt. Printf ("Value of a:%d\n", a)
a++
}
}


Let's compile and run the above program, which will produce the following results:

Value of A:10
value of a:11
value of a:12 value of a:13 value of a:14 value of
a:16
value of A:17
value of a:18
value of a:19

Infinite loop

A loop becomes infinitely cyclic if the condition is never false. For loops are traditionally used for this purpose. Because there is no three expression required to form A for loop, you can form a dead loop by leaving the condition, using an empty expression, or passing true.

Copy Code code as follows:

Package Main

Import "FMT"

Func Main () {
For true {
Fmt. Printf ("This loop would run forever.\n");
}
}


When the conditional expression is not present, it is assumed to be true. You may have an initialization and incremental expression, but C programmers are more commonly used for (;; ) structure to represent an infinite loop.

Note: You can terminate an infinite loop by pressing CTRL + C.

Related Article

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.