Data structure and algorithm (Golang implementation) (3) A simple introduction to Golang-flow control statement

Flow control statement

In computer programming languages, flow control statements are very important, allowing the machine to know when to do something, and to do it a few times. There are mainly conditional and looping statements.

GolangThere is only one kind of loop:, foronly one kind of judgment:, ifand there is a special switchconditional selection statement.

First, the conditional statement

for example:

    // 判断语句
    if a > 0 {
        fmt.Println("a>0")
    } else {
        fmt.Println("a<=0")
    }

When a > 0the print a>0, or print a<=0. The conditions a > 0do not require parentheses.

The conditional statement indicates what to do, otherwise, what to do.

Several forms of judgment are:

if a > 0{

}

Only if.

if a > 0{

}else{

}

There are ifas well else.

if a > 0{

}else if a == 0 {

}else{

}

Can be mixed in the middle else if.

If there are too many intermediate conditions, you can use switchconditional statements:

    num := 4

    switch num {
    case 3:
        fmt.Println(3)
    case 4:
        fmt.Println(4)
    case 5:
        fmt.Println(5)
    default:
        fmt.Println("not found")

Such statements will be casejudged one by one. If one casemeets the condition, then enter the caseexecution instruction, otherwise enter default.

The above num := 4will enter case 4, print the number 4 and end. If it does num := 5, it will print the number 5 and end. If it does num := 8, the string not found will be printed.

Second, the loop statement

loop statement:

    // 循环语句
    for i := 9; i <= 10; i++ {
        fmt.Printf("i=%d\n", i)
    }

Among them iare local variables. The value is assigned before the loop for the first time 9, and then it is judged whether the i<=10condition is satisfied . If it is satisfied , the loop print is added 1, and it will be added after each loop , that is i++, then continue to judge whether the condition is met.

The form is:

    for 起始状态; 进入循环需满足的条件; 每次循环后执行的指令 {
    }

You can also endlessly loop:

    // 死循环语句
    a = 0
    for {
        if a >= 10 {
            fmt.Println("out")
            // 退出循环
            break
        }

        a = a + 1
        if a > 5 {
            continue
        } 

        fmt.Println(a)
    }

Infinite loop directly for {}, no need to add back condition, and then when a>=10the time out of the loop can be used breakto indicate to jump out for {}, for a > 5we do not want to print out the values, you can use continueskip the next sentence fmt.Println(a), once again advance into the circulation.

切片And 字典can be used to cycle through the data:


    e := []int64{1, 2, 3}                 // slice
    f := map[string]int64{"a": 3, "b": 4} // map

    // 循环切片
    for k, v := range e {
        fmt.Println(k, v)
    }

    // 循环map
    for k, v := range f {
        fmt.Println(k, v)
    }

The result of the traversal of the slice is: data subscript, data, and the result of the dictionary traversal: the key of the data, the value of the data:

0 1
1 2
2 3
3 3

a 3
b 4
f 5

Series article entry

I am the star Chen, Welcome I have personally written data structures and algorithms (Golang achieve) , starting in the article to read more friendly GitBook .

Published 13 original articles · Likes0 · Visits 96

Guess you like

Origin blog.csdn.net/m0_46803965/article/details/105563270