Golang Learning note -1.9 for loop

Source: Internet
Author: User

This is the Nineth chapter of Golang Language Learning Tutorial
The go language is only for this kind of loop statement. The For Loop has three forms, and only one of them uses a semicolon.

    • Similar to other languages for
for init; condition; post { }
    • Same as while
for condition { }
    • and other languages for {;;} Similar
for { }

Init: Usually an assignment expression that assigns an initial value to a loop variable
Condition: Cyclic control conditions, relational expressions, or logical expressions
Post: Typically an assignment expression that increments or decrement a loop variable

Here are a few examples to understand the loop
Example 1

package mainimport "fmt"func main(){    for i :=1; i <=10; i++{ //i=1,且i<=10,每次循环+1        fmt.Printf(" %d", i)    }}

In the above example, the init,condition,post is defined as a semicolon, respectively ; .

The initialization statement executes only once, and after the loop is initialized, the loop condition is checked, and if the result is true , the loop body is executed, the { } post statement executes after each successful loop iteration, and after the post is executed, the condition is checked again, and if the result is true , the loop resumes execution. Otherwise the loop ends.

Example 2

package mainimport "fmt"func main(){    j := 0    for ; j <= 10; {  //两个 ; 可以省略        fmt.Printf(" %d", j)        j += 2    }}

In the above example, the initialization statements and post statements are omitted, j initialized outside the for loop, and the 0 loop executes as long as the condition j <= 10 is true, and J increments from 2 in the loop.
The above program prints all the even numbers from 0 to 10:0 2 4 6 8 10

Example 3

package mainimport "fmt"func main(){    for {        fmt.Println("Hello,world!")    }}

The above program is an infinite loop program, will not stop the outputHello,world!

Break

A break is used to abruptly abort a for loop before it completes normal execution, and then the program begins executing a line of code in the For loop.
Example 4

package mainimport "fmt"func main(){    for a := 0; a <= 10; a++{        if a >5 {            break        }        fmt.Printf(" %d", a)    }    fmt.Println("\nthis go is stop")}

The above procedures at the beginning of the cycle to determine a > 5 whether the condition is set up, if not, continue to print a value, continue to loop; if it is, break terminate the loop and proceed with the following print this go was stop
The results of the loop printing are:

0 1 2 3 4 5

This go is stop

Continue

continueTo jump out of the current loop, continue all loops are no longer executed in the loop after the statement, and the loop continues in the next loop.
Example 5

package mainimport "fmt"func main(){    for b := 0; b <= 10; b++{        if b%2 == 0 {            continue        }        fmt.Printf(" %d", b)    }}

The above procedure at the beginning of the cycle to determine whether the remainder of B divided by 2 is 0 ( b%2 == 0 ), if the result is set, then jump out of this cycle, the subsequent statement will not be executed; if not, continue to execute the following statement.
The result of the loop printing is:
1 3 5 7 9

Example 6
Multiple variables can be declared and manipulated in a For loop:

package mainimport "fmt"func main(){    for yes, no :=10,1; yes <=20 && no <=10 ; yes, no = yes+1, no+1  {        fmt.Printf("\n%d * %d = %d", yes,no,yes * no )    }}

The above program declares the value of Yes and no to && ensure that the value of Yes is less than 20 and no value is less than 10, and yes and no increment 1 after each cycle
The result of the loop printing is:

10 * 1 = 10
11 * 2 = 22
12 * 3 = 36
13 * 4 = 52
14 * 5 = 70
15 * 6 = 90
16 * 7 = 112
17 * 8 = 136
18 * 9 = 162
19 * 10 = 190

The above is the study Golang circulation chapter

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.