5.Go by Example:for

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


For was Go ' s only looping construct. Here is three basic types of for loops.

The most basic type, with a single condition.

A Classic Initial/condition/after for loop.

For without a condition would loop repeatedly until you break out of the the loop or return from the enclosing function.


Package Mainimport "FMT" Func Main () {     I: = 1 for    i <= 3 {        FMT. Println (i)        i = i + 1    } for     J: = 7; J <= 9; j + + {        FMT. Println (j)    } for    {        FMT. Println ("loop")        Break    }}


Translated:
The FOR keyword is the only loop structure in the go language, the following is the three basic type structure for a for loop

The most basic is a single condition

One is the initial value/condition/Then the self-processing condition for the For loop

The For loop will loop until the break is out of the loop for no condition, or the perimeter function returns


Package Mainimport "FMT" Func main () {i: = 1for I <= 3 {//= equivalent to While loop FMT. PRINTLN (i) i = i + 1}for j: = 7; J <= 10; J + + {//traditional for loop fmt. Println (j)}for {//similar to do ... While Loop FMT. Println ("Loop") Break}}

Run results
Go Run for.go
1
2
3
7
8
9
10

Loop


Original address

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.