8. Loop

Source: Internet
Author: User

The Go language has only one loop body, which is a go loop.

The basic for loop looks like C or Java. But it does not have parentheses () and must not have them. braces {} are mandatory.

 
1 PackageMain
2
3Import "FMT"
4
5Func main (){
6 Sum: = 0
7 For I: = 0; I <10; I ++ {
8 Sum + = I
9 }
10FMT. println (SUM)
11}
45

Just like C or Java, You can omit the preceding and subsequent statements.

 
1 PackageMain
2
3Import "FMT"
4
5Func main (){
6 Sum: = 1
7 For; sum <1000 ;{
8 Sum + = sum
9 }
10FMT. println (SUM)
11}
1024

You can delete the semicolon in that position. The while in C language is replaced with for in the go language.

 
1 PackageMain
2
3Import "FMT"
4
5Func main (){
6 Sum: = 1
7 For sum <1000 {
8 Sum + = sum
9 }
10FMT. println (SUM)
11}
1024

If you delete the loop condition statement, it is a wireless loop.

 
PackageMain

Func main (){
For ;;{
}
}
 
[Process took too long]

There are no statements and the semicolon is omitted. It is a compact infinite loop.

 
1 PackageMain
2
3Func main (){
4 For {
5 }
6}

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.