# 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.

`Golang`

There is only one cycle:`for`

There is only one judgment:`if`

And there’s a special one`switch`

Conditional selection statement.

## 1、 Conditional statement

for instance:

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

When`a > 0`

When printing`a>0`

Otherwise, print`a<=0`

。 Conditions`a > 0`

You 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{
}
```

only`if`

。

```
if a > 0{
}else{
}
```

Yes`if`

as well as`else`

。

```
if a > 0{
}else if a == 0 {
}else{
}
```

It can be mixed in the middle`else if`

。

If there are too many intermediate conditions, you can use`switch`

Conditional 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 from`case`

Judge one by one, if you find one`case`

If it meets the requirements, enter the`case`

Execute the command, otherwise enter`default`

。

above`num := 4`

Will enter`case 4`

After printing the number 4. If`num := 5`

The number 5 will be printed. If`num := 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)
}
```

among`i`

Is a local variable, and the value is assigned before the first loop`9`

And then judge whether it is satisfied`i<=10`

If the condition is satisfied, the system will enter the circulation printing, and will be added after each cycle`1`

That is to say`i++`

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 direct`for {}`

And then you don’t need to add a condition to it`a>=10`

You can use the`break`

, means to jump out`for {}`

For`a > 5`

, we don’t want to print out the value, we can use`continue`

Skip subsequent statements`fmt.Println(a)`

To enter the loop again in advance.

`section`

and`Dictionaries`

You 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 myself**Data 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