6. Toad Notes Go language--for loop

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

6. Toad Notes Go language--for loop

For

Go has only one loop structure-the ' for ' Loop.

The basic for loop does not have to be "()" (or even forced to use them), it looks like it does in C or Java, and ' {} ' is required.

Package Main

Import "FMT"

Func Main () {

Sum: =0

For i:= 0; I < 10; i++ {

sum+= I

}

Fmt. PRINTLN (SUM)

}

Perform:

45

As in C or Java, you can leave the front and back statements empty.

Package Main

Import "FMT"

Func Main () {

Sum: =1

For sum< 1000 {

sum+= sum

}

Fmt. PRINTLN (SUM)

}

Based on this, you can omit the semicolon: C while in Go is called ' for '.

Package Main

Import "FMT"

Func Main () {

Sum: =1

For sum< 1000 {

sum+= sum

}

Fmt. PRINTLN (SUM)

}

Dead loop

If the loop condition is omitted, the loop does not end, so it is possible to express the dead loop in a more concise form.

Package Main

Func Main () {

for {

}

}

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.