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.
Golang
There is only one kind of loop:, for
only one kind of judgment:, if
and there is a special switch
conditional selection statement.
First, the conditional statement
for example:
// 判断语句
if a > 0 {
fmt.Println("a>0")
} else {
fmt.Println("a<=0")
}
When a > 0
the print a>0
, or print a<=0
. The conditions a > 0
do 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 if
as 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 switch
conditional 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 case
judged one by one. If one case
meets the condition, then enter the case
execution instruction, otherwise enter default
.
The above num := 4
will 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 i
are local variables. The value is assigned before the loop for the first time 9
, and then it is judged whether the i<=10
condition 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>=10
the time out of the loop can be used break
to indicate to jump out for {}
, for a > 5
we do not want to print out the values, you can use continue
skip 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 .
- Data structure and algorithm (Golang implementation) (1) A simple introduction to Golang-Preface
- Data structures and algorithms (Golang implementation) (2) A simple introduction to Golang-packages, variables and functions
- Data structure and algorithm (Golang implementation) (3) A simple introduction to Golang-flow control statement
- Data structures and algorithms (Golang implementation) (4) A simple introduction to Golang-structures and methods
- Data structure and algorithm (Golang implementation) (5) A simple introduction to Golang-interface
- Data structure and algorithm (Golang implementation) (6) A simple introduction to Golang-concurrency, coroutines and channels
- Data structure and algorithm (Golang implementation) (7) A simple introduction to Golang-standard library
- Data Structure and Algorithm (Golang Implementation) (8.1) Basic Knowledge-Preface
- Data Structure and Algorithm (Golang Implementation) (8.2) Basic Knowledge-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-the main method of algorithm complexity
- Data Structures and Algorithms (Golang Implementation) (11) Common Data Structures-Preface
- Data structures and algorithms (Golang implementation) (12) Common data structures-linked lists
- Data structures and algorithms (Golang implementation) (13) Common data structures-variable-length arrays
- Data structures and algorithms (Golang implementation) (14) Common data structures-stack and queue
- Data Structures and Algorithms (Golang Implementation) (15) Common Data Structures-List
- Data Structures and Algorithms (Golang Implementation) (16) Common Data Structures-Dictionary
- Data structures and algorithms (Golang implementation) (17) Common data structures-trees
- Data Structure and Algorithm (Golang Implementation) (18) Sorting Algorithm-Preface
- Data structure and algorithm (Golang implementation) (19) Sorting algorithm-bubble sorting
- Data structure and algorithm (Golang implementation) (20) Sorting algorithm-selection sorting
- Data structure and algorithm (Golang implementation) (21) Sorting algorithm-insertion sort
- Data structure and algorithm (Golang implementation) (22) Sorting algorithm-Hill sorting
- Data structure and algorithm (Golang implementation) (23) Sorting algorithm-merge sorting
- Data structure and algorithm (Golang implementation) (24) Sorting algorithm-priority queue and heap sorting
- Data structure and algorithm (Golang implementation) (25) Sorting algorithm-quick sorting
- 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) Finding algorithm-2-3 tree and left-leaning red-black tree
- Data structure and algorithm (implemented by Golang) (30) Finding algorithm-2-3-4 tree and ordinary red-black tree