The jump statements include: 1. The break statement is used to exit the loop or exit a switch statement to allow the program to continue executing the code after the loop. The syntax is "break;"; 2. The continue statement is used to exit this time. Loop and start the next loop, the syntax is "continue;"; 3. Combined with the label to jump to the specified label statement, the syntax is "label:"; 4. The goto statement is used to unconditionally transfer to the specified line in the program, Syntax "goto tag;... ...tag: expression;".
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
Jump statements in Go flow control
break and continue statements
and others Like programming languages, the Go language supports breaking out of the loop through the break statement and entering the next loop through the continue statement.
We have already demonstrated the basic usage examples of break in the previous tutorial. The default scope of break is the innermost loop body where the statement is located:
arr := [][]int{{1,2,3},{4,5,6},{7,8,9}} for i := 0; i < 3; i++ { for j := 0; j < 3; j++ { num := arr[i][j] if j > 1 { break } fmt.Println(num) } }
For example, the break here The meaning is to exit the innermost loop when j > 1, otherwise print the number at the current position.
continue is used to ignore the remaining loop body and directly enter the next loop process:
arr := [][]int{{1,2,3},{4,5,6},{7,8,9}} for i := 0; i < 3; i++ { for j := 0; j < 3; j++ { num := arr[i][j] if j > 1 { break } else { continue } fmt.Println(num) } }
If we rewrite the program like this, the above code will not print any value, because the continue statement will Ignore the subsequent code and go directly to the next loop.
tag
The difference between break and continue in Go language and other languages is that it supports combining with tags to jump to the specified tag statement, thereby changing these two The default jump logic of the statement, the label statement is declared through the tag:
:
arr := [][]int{{1,2,3},{4,5,6},{7,8,9}} ITERATOR1: for i := 0; i < 3; i++ { for j := 0; j < 3; j++ { num := arr[i][j] if j > 1 { break ITERATOR1 } fmt.Println(num) } }
In this way, the break statement that originally exited the current loop body now jumps to the ITERATOR1 label corresponding position, so the corresponding print result is:
1 2
Because at this time break will jump out of the outer loop directly. If break is changed to continue, the print result is as follows:
1 2 4 5 7 8
goto statement
The goto statement is opposed by most linguists, and everyone is warned not to use it, because it can easily cause confusion in the code logic, which can lead to hard-to-find bugs. However, the Go language still supports the goto keyword. The semantics of the goto statement are very simple, which is to jump to a certain label within this function, such as:
arr := [][]int{{1,2,3},{4,5,6},{7,8,9}} for i := 0; i < 3; i++ { for j := 0; j < 3; j++ { num := arr[i][j] if j > 1 { goto EXIT } fmt.Println(num) } } EXIT: fmt.Println("Exit.")
When the first time j > 1
conditions, the code will jump to the location specified by the EXIT
tag and continue subsequent code execution, so the output of the above code is:
1 2 Exit.
[Related recommendations: Go video tutorial、programming teaching】
The above is the detailed content of What are the jump statements in go language?. For more information, please follow other related articles on the PHP Chinese website!