Home > Backend Development > Golang > What are the jump statements in go language?

What are the jump statements in go language?

青灯夜游
Release: 2022-12-26 17:51:03
Original
4683 people have browsed it

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;".

What are the jump statements in go language?

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)
    }
}
Copy after login

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)
    }
}
Copy after login

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)
    }
}
Copy after login

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
Copy after login

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
Copy after login

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.")
Copy after login

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.
Copy after login

[Related recommendations: Go video tutorialprogramming 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template