Go Learning-control statements

Source: Internet
Author: User
Tags variable scope
This is a creation in Article, where the information may have evolved or changed.

If a: = 1; A > 1 {//define variable A,a only valid in the IF statement
Fmt. Println ("a > 1")
} else {
If a: = 10; A > 1 {
Fmt. Println (a)//result is 10, and C variable scope is consistent
} else {
Fmt. Println (a)
}
}

Three types of For

Func Main () {
Count: = 3
For I: = 0; I < count; i++ {//Classic form
Fmt. Println (i)
}
I: = 0
For I < count {//Like while in C, with terminating conditions only
Fmt. Println (i)
i++
}
i = 0
For {//Dead loop
If I < count {
Fmt. Println (i)
i++
} else {
Break
}
}
}

Switch usage:
Func Main () {
Count: = 1
Switch Count {
Case 1:
Fmt. Println (count)
Fallthrough//Continue checking the next case, shielding the break function
Case 2:
Fmt. Println (count)//case comes with break function
Case 3:
Fmt. Println (count)
Default
Fmt. Println (count)
}
}

Func Main () {
Count: = 1
Switch {//switch does not have a constant, and the If......else If......esle function in C is consistent
Case count = = 1://case need to use logical expressions
Fmt. Println (count)
Fallthrough//Continue checking the next case, shielding the break function
Case Count <= 1:
Fmt. Println (count)//case comes with break function
Case Count >= 1:
Fmt. Println (count)
Default
Fmt. Println (count)
}
}

Break and label

Func Main () {
J:
For j: = 0; J < 5; J + + {
For I: = 0; I < 10; i++ {
If I > 6 {
Break J//Label J is located on the first layer for loop, break J is equivalent to jumping out of the first layer for loop
}
Fmt. Println (i)
}
}
}

Continue and tags

Func Main () {
J:
For j: = 0; J < 5; J + + {
Fmt. Println ("--------")
For I: = 0; I < 10; i++ {
If I > 6 {
Continue J//Tag J is located in the first layer for loop, continue J is equivalent to skipping its following statement and jumping to the first layer for continuation of the first layer loop (the value of the first loop condition J is still incremented)
}
Fmt. Println (i)
}
Fmt. Println ("+++++++")//continue the first layer of the body's language will not be executed, note the difference from C
}
}

Goto and Label

Func Main () {
J:
For j: = 0; J < 5; J + + {
Fmt. Println ("--------")
For I: = 0; I < 10; i++ {
If I > 6 {
Goto J//goto to the first-level for loop, the value of J is re-assigned to 0, at which time the loop is an infinite loop, and the difference between the Continue is: Continue continues the loop, Goto restarts the loop
}
Fmt. Println (i)
}
Fmt. Println ("+++++++")
}
}

When using Goto, be careful to avoid the dead loop, Goto as far as possible to the following code

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.