Golang language for Loop statement usages _golang

Source: Internet
Author: User

The example in this article describes the use of a For loop statement in the Golang language. Share to everyone for your reference. The specific analysis is as follows:

A For loop is used to traverse an array or a number. There are also byte and rune two ways to iterate through a string with a For loop. The first is byte and the second is rune.

Copy Code code as follows:
Package Main
Import (
"FMT"
)
Func Main () {
S: = "ABC Kanji"
For I: = 0; I < Len (s); i++ {
Fmt. Printf ("%c,", S[i])
}
Fmt.println ()
For _, R: = Range S {
Fmt. Printf ("%c,", R)
}
}

Output results:
A,b,c,d,e,f,d,ä,¸, the
In ABCDEFD

Comparison

Copy Code code as follows:
Package Main
Import "FMT"
Func Main () {
Sum: = 0
For I: = 0; I < 10; i++ {
sum = i
}
Fmt. PRINTLN (SUM)
}

As in C, you can leave the front and back statements blank.
Copy Code code as follows:
Package Main
Import "FMT"
Func Main () {
Sum: = 1
for; Sum < 1000; {
sum = Sum
}
Fmt. PRINTLN (SUM)
}

Based on this, you can omit semicolons:
Copy Code code as follows:
Package Main
Import "FMT"
Func Main () {
Sum: = 1
For sum < 1000 {
sum = Sum
}
Fmt. PRINTLN (SUM)
}
If the loop condition is omitted, it is a dead loop source.
[Code]package Main
Func Main () {
for;; {
}
}

To avoid redundancy, semicolons can be omitted, so a dead loop can be expressed succinctly.
Copy Code code as follows:
Package Main
Func Main () {
for {
}
}

I hope this article will be helpful to everyone's Golang language programming.

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.