Data structure and algorithm (golang Implementation) (3) simple introduction to golang process control statements

Time:2020-7-5

Process control statement

In computer programming language, process control statements are very important, which can let the machine know when to do what and how many times to do it. There are mainly conditional and loop statements.

GolangThere is only one cycle:forThere is only one judgment:ifAnd there’s a special oneswitchConditional selection statement.

1、 Conditional statement

for instance:

//Judgment statement
    if a > 0 {
        fmt.Println("a>0")
    } else {
        fmt.Println("a<=0")
    }

Whena > 0When printinga>0Otherwise, printa<=0。 Conditionsa > 0You don’t need parentheses.

Conditional statements indicate what if and what to do, otherwise, what to do.

Several forms of judgment are as follows:

if a > 0{

}

onlyif

if a > 0{

}else{

}

Yesifas well aselse

if a > 0{

}else if a == 0 {

}else{

}

It can be mixed in the middleelse if

If there are too many intermediate conditions, you can useswitchConditional statement:

    num := 4

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

This statement will start fromcaseJudge one by one, if you find onecaseIf it meets the requirements, enter thecaseExecute the command, otherwise enterdefault

abovenum := 4Will entercase 4After printing the number 4. Ifnum := 5The number 5 will be printed. Ifnum := 8, the string not found will be printed.

2、 Loop statement

Loop statement:

//Loop statement
    for i := 9; i <= 10; i++ {
        fmt.Printf("i=%d\n", i)
    }

amongiIs a local variable, and the value is assigned before the first loop9And then judge whether it is satisfiedi<=10If the condition is satisfied, the system will enter the circulation printing, and will be added after each cycle1That is to sayi++And then continue to determine whether the conditions are met.

In the form of:

For start state; conditions to be satisfied to enter a loop; instructions executed after each loop{
    }

You can also loop

//Loop statement
    a = 0
    for {
        if a >= 10 {
            fmt.Println("out")
            //Exit loop
            break
        }

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

        fmt.Println(a)
    }

Dead loop directfor {}And then you don’t need to add a condition to ita>=10You can use thebreak, means to jump outfor {}Fora > 5, we don’t want to print out the value, we can usecontinueSkip subsequent statementsfmt.Println(a)To enter the loop again in advance.

sectionandDictionariesYou can use loops to traverse data:

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

    //Circular section
    for k, v := range e {
        fmt.Println(k, v)
    }

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

The results of slice traversal are: Data subscript, data, dictionary traversal results are: data key, data value:

0 1
1 2
2 3
3 3

a 3
b 4
f 5

Entry to series

I’m Chen Xingxing. Welcome to read it myselfData structure and algorithm (golang Implementation), the article started with a more user-friendly gitbook.

  • Data structure and algorithm (implementation of golang) (1) introduction to golang
  • Data structure and algorithm (golang Implementation) (2) a simple introduction to golang packages, variables and functions
  • Data structure and algorithm (golang Implementation) (3) simple introduction to golang process control statements
  • Data structure and algorithm (golang Implementation) (4) simple introduction to golang structure and method
  • Data structure and algorithm (golang Implementation) (5) simple introduction to golang interface
  • Data structure and algorithm (golang Implementation) (6) a simple introduction to golang concurrency, coprogram and channel
  • Data structure and algorithm (golang Implementation) (7) simple introduction to golang standard library
  • Basic knowledge of data structure and algorithm (golang Implementation) (8.1) – Introduction
  • Data structure and algorithm (golang Implementation) (8.2) Basics – divide and conquer and recursion
  • Data structure and algorithm (golang Implementation) (9) basic knowledge algorithm complexity and progressive symbol
  • Data structure and algorithm (golang Implementation) (10) basic knowledge algorithm complexity main method
  • Data structure and algorithm (golang Implementation) (11) common data structure – Preface
  • Data structure and algorithm (golang Implementation) (12) common data structure linked list
  • Data structure and algorithm (golang Implementation) (13) common data structure variable length array
  • Data structures and algorithms (golang Implementation) (14) common data structures stacks and queues
  • Data structures and algorithms (golang Implementation) (15) common data structures – Lists
  • Data structure and algorithm (golang Implementation) (16) common data structure dictionary
  • Data structure and algorithm (golang Implementation) (17) common data structure tree
  • Data structure and algorithm (golang Implementation) (18) sorting algorithm – Preface
  • Data structure and algorithm (golang Implementation) (19) sorting algorithm bubble sort
  • Data structure and algorithm (golang Implementation) (20) sorting algorithm selection sort
  • Data structure and algorithm (golang Implementation) (21) sort algorithm insert sort
  • Data structure and algorithm (golang Implementation) (22) sorting algorithm Hill sort
  • Data structure and algorithm (golang Implementation) (23) sorting algorithm merge sort
  • Data structure and algorithm (golang Implementation) (24) sorting algorithm priority queue and heap sort
  • Data structure and algorithm (golang Implementation) (25) sorting algorithm quick sort
  • Data structure and algorithm (golang Implementation) (26) lookup algorithm hash table
  • Data structure and algorithm (golang Implementation) (27) search algorithm binary search tree
  • Data structure and algorithm (golang Implementation) (28) search algorithm AVL tree
  • Data structure and algorithm (golang Implementation) (29) search algorithm-2-3 tree and left-handed red black tree
  • Data structure and algorithm (golang Implementation) (30) search algorithm-2-3-4 tree and common red black tree

Recommended Today

ASP.NET Example of core MVC getting the parameters of the request

preface An HTTP request is a standard IO operation. The request is I, which is the input; the responsive o is the output. Any web development framework is actually doing these two things Accept the request and parse to get the parameters Render according to the parameters and output the response content So let’s learn […]