This is a creation in Article, where the information may have evolved or changed.
Process Control is the instruction to change the order in which the program runs, either by running a different location, or by selecting a run in a two-segment (or multiple-segment) program. The process control statements of programming language are used to set the order of calculation execution and to establish the logical structure of the program. It can be said that the Process Control statement is the skeleton of the entire program.
Conditional statements
Conditional judgment is the result of the specified variable or expression, determine the subsequent operation of the program, according to whether the specified conditions are established, determine the subsequent procedures.
For go conditional statements, there are two ways of doing this:
- Single condition judgment: If-else, can also combine multiple if-else instruction, carry on more complicated condition judgment.
- Select a condition to judge: Switch-case.
If Else
The IF statement of Go consists of a Boolean expression followed by one or more statements. An If statement can use an optional else statement, and an expression in the Else statement executes when the Boolean expression is false.
The usual format is:
if 布尔表达式 { trueelse { false 时执行 */}
Where Go does not require parentheses () outside of the IF statement expression, and curly braces {} are required. At the same time {must be immediately followed by the IF statement, non-wrapped, otherwise compiled or error –missing condition in if statement.
The following code:
"fmt"func main() { /* 定义局部变量 */ a10 /* 使用 if 语句判断布尔表达式 */ ifa20 { /* 如果条件为 true 则执行以下语句 */ fmt.Printf("a 小于 20\n") else { /* 如果条件为 false 则执行以下语句 */ fmt.Printf("a 不小于 20\n") } fmt.Printf("a 的值为 : %d\n"a)}
In addition, the IF or else if statements of go can embed one or more if or else if statements.
PackageMainImport "FMT"funcMain () {/ * Define Local Variables * / varAint= - varBint= $ / * Judging condition * / ifA = = -{FMT. Printf ("A value is:%d\n", a)}Else ifA = = -{/ * If condition statement is true * / ifb = = ${/ * If condition statement is true * /Fmt. Printf ("A has a value of 200\n and a value of B is")}} FMT. Printf ("A value is:%d\n", a) fmt. Printf ("b value is:%d\n", b)}
Short Statement of If
The IF statement can execute a simple statement before the conditional expression.
PackageMainimport ("FMT" "Math") Func Pow (x, N, Lim Float64) {ifV: = Math. Pow (x, n); V < Lim {Fmt. Printf ("1.math. Pow (%f, %f):%f \ n ",x, N, v)}Else{FMT. Printf ("2.math. Pow (%f, %f):%f \ n ",x, N, v)} FMT. Println (Lim)}func main () {POW (3,2,Ten) Pow (3,3, -)}
This defines a variable V and initializes the assignment before the IF-judgment expression. It is important to note that the variable scope declared by the statement is only within the IF .
Switch case
Go's switch and other language functions are basically consistent, are used to perform different actions based on different conditions, each case branch is unique, from the top straight down to test, until the match.
The specific syntax also has go features:
- There is no need to add a break after the match first
- The expression after switch is not required
- You can write an expression in case.
- Multiple result options can appear in a single case
- The left curly brace {must be in the same row as switch. This is common in go.
There is a special keyword fallthrough, and only if the Fallthrough keyword is explicitly added in the case, the next case immediately following the execution will continue
In general, the syntax of the switch statement in the Go programming language is as follows:
switch var1 { case val1: ... case val2: ... default: ...}
Variable var1 can be of any type, while Val1 and val2 can be any value of the same type. Types are not limited to constants or integers, but must be of the same type, or the final result is an expression of the same type.
PackageMainImport "FMT"funcMain () {/ * Define Local Variables * / varGradestring="B" varMarksint= - SwitchMarks { Case -: Grade ="A" Case the: Grade ="B" Case -, -, -: Grade ="C" default: Grade ="D"}Switch{ CaseGrade = ="A": FMT. Printf ("Excellent!\n") CaseGrade = ="B", Grade = ="C": FMT. Printf ("good \ n") CaseGrade = ="D": FMT. Printf ("pass \ n") CaseGrade = ="F": FMT. Printf ("less than a lattice \ n")default: FMT. Printf ("Bad ")} FMT. Printf ("Your rank is%s\n.", Grade)}
The first switch above is basically consistent with the syntax switch in other common languages. The second one has a go feature. There is no expression behind switch, and the case statement can also use an expression to determine
PackageMainImport "FMT"funcMain () {/ * Define Local Variables * / varGradestring="B" Switch{ CaseGrade = ="A": FMT. Printf ("Excellent!\n")Fallthrough CaseGrade = ="B", Grade = ="C": FMT. Printf ("good \ n")Fallthrough CaseGrade = ="D": FMT. Printf ("pass \ n") CaseGrade = ="F": FMT. Printf ("less than a lattice \ n")Fallthrough default: FMT. Printf ("Bad ")} FMT. Printf ("Your rank is%s\n.", Grade)}
As you can see, when the statement is established, the next case is executed if Fallthrough is added.
Looping statements
The loop statement in the go language only supports the FOR keyword, while the while and do-while structures are not supported, but some of the notation is similar. The basic use of the keyword for is very close to C and C + +.
The basic for loop consists of three parts, separated by semicolons:
- Initialization statement: Executed before the first iteration
- Conditional expression: Evaluate before each iteration
- Post statement: Executes at the end of each iteration
Basic form
The For loop of the Go language has 3 forms
1、forpost { }
Note that conditional statements are also not required ().
"fmt"func main() { sum0 for0100; i++ { sum += i } fmt.Println(sum)}
The above code implements the operation of 1 plus to 100.
2、forcondition { }
Some of this implementation is like while, and is executed as long as the condition is established.
package mainimport"fmt"func main() { varint = 15 varint = 10 for a < b { a++ fmt.Printf("a 的值为: %d\n", a) }}
3、for{ }
This definition, in fact, is an infinite loop. If the loop condition is omitted, the loop does not end.
For the for condition, you can also use the range format to iterate through the slice, map, array, string, and so on.
For range
forvalue := range oldMap { value}
package mainimport"fmt"func main() { [6]int{1, 2, 3, 5} forrange numbers { fmt.Printf("第 %d 位 x 的值 = %d\n", i, x) }}
Loop control Statements
The GO language supports the following types of loop control statements:
- Break statements are often used to interrupt the current for loop or to jump out of a switch statement
package mainimport"fmt"func main() { /* 定义局部变量 */ varint = 10 /* for 循环 */ for a < 20 { fmt.Printf("a 的值为 : %d\n", a) a++ if a > 15 { /* 使用 break 语句跳出循环 */ break } }}
The
-Continue statement skips the remaining statements of the current loop, and then proceeds to the next round of loops.
package mainimport "FMT" Func Main () {/* define local Variables */ var a int = Span class= "Hljs-number" >10 /* for Loop */ for a < 20 {if
a = = 15 { /* Skip this cycle */ a = a + 1 continue} fmt. PRINTF (the value of , a ) a + +}}
can see that 15 is not being called out. The
-goto statement transfers control to the marked statement. Also known as a jump statement.
However, the use of goto statements is generally not advocated in structured programming, which makes it difficult to understand and debug programs because of the confusion of program flow.
"fmt"func main() { /* 定义局部变量 */ a10 /* 循环 */LOOP: fora20 { ifa15 { /* 跳过迭代 */ aa1 goto LOOP } fmt.Printf("a的值为 : %d\n"a) a++ }}