My Go Language learning Tour three: For loop in go language

Source: Internet
Author: User
Tags terminates

This but embarrassed me, I have been walking in front of the web of the program Ape, a variety of loops used up handy. Suddenly found that go here unexpectedly does not work, check only to find out, the original go for is this write.

For I:=0;I<10;I++{FMT. Printf ("%d\n", I);}
A non-Chang way of writing, I used to feel really without parentheses comfortable.

Next. See a lot of other examples of for?

Go's for loop has three different forms. Only one of them uses a semicolon. For Init; Condition Post {}← and C for the same as for condition {}← and while as for {}← and C for (;;) (dead Loop) short declarations make it easier to declare an ordinal variable in a loop. Sum: = 0for I: = 0; I < 10; i++ {sum + = I←sum = sum + i's simplified notation}←i instances end in the loop, because Go has no comma expression, and + + and – are statements instead of expressions, assuming you want to run multiple variables in for, you should use parallel assignments.

Reverse A For I, J: = 0, Len (a)-1; I < J; I, j = i+1, j-1 {← Parallel assignment a[i], a[j] = A[j], a[i]← here is also}break and continue using break to exit the loop prematurely, break terminates the current loop. For I: = 0; I < 10; i++ {If i > 5 {break← terminates this loop and only prints 0 to 5}println (i)} when looping nested loops. Ability to specify labels after break.

Use the label to determine which loop is terminated: j:for J: = 0; J < 5; J + + {for I: = 0; i <; i++ {if-i > 5 {break j← now terminates the J-loop, instead of the}PRINTLN (i)}}, which uses continue to get the loop into the next iteration and skips over all the remaining code. The following loop prints 0 to 5.

For I: = 0; I < 10; i++ {If i > 5 {continue← skips all code in the loop println (i) range reserved word range is available for looping. It can be in slice, array, string, map, and channel. Range is an iterator that is invoked when it is called. Returns a key-value pair from the contents of its loop. Depending on the content, range returns different things. When looping over slice or arrays. Range returns the ordinal as the key and the corresponding contents of the ordinal as the value. Consider this code: list: = []string{"A", "B", "C", "D", "E", "F"}. . 0for k, V: = Range list {.. 1//do what you want to do with K and V.. 2}

My Go Language learning Tour three: For loop in go language

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.