Home  >  Article  >  Backend Development  >  Master the use of flow control statements in Golang

Master the use of flow control statements in Golang

王林
王林Original
2023-12-23 13:21:39491browse

Master the use of flow control statements in Golang

To understand the flow control statements and their usage in Golang, specific code examples are required

Golang is a programming language developed by Google. It is powerful and flexible, with Fast compilation and execution features. Flow control statements play a vital role in programming. They can be used to determine the execution path and logic of the program.

In Golang, commonly used flow control statements include conditional statements (if and switch), loop statements (for and range) and jump statements (break, continue and goto). Below we will introduce their usage one by one and provide specific code examples.

Conditional statements

Conditional statements are used to decide whether to execute a certain code block or skip it based on different conditions. In Golang, the if statement is the most commonly used conditional statement. Its general grammatical structure is:

if condition {
    // code to be executed if condition is true
} else {
    // code to be executed if condition is false
}

The following is an example of using an if statement:

package main
 
import "fmt"
 
func main() {
    var age int
    fmt.Print("请输入你的年龄: ")
    fmt.Scan(&age)
 
    if age >= 18 {
        fmt.Println("你是成年人")
    } else {
        fmt.Println("你是未成年人")
    }
}

In this example, if the input age is greater than or equal to 18, the output is "You are an adult ”, otherwise output “You are a minor”.

Loop statement

Loop statement is used to repeatedly execute a block of code until certain conditions are met. In Golang, the most commonly used loop statement is the for loop. Its general syntax structure is:

for initialization; condition; increment {
    // code to be executed in each iteration
}

The following is an example of using a for loop to calculate the cumulative sum of 1 to 10:

package main
 
import "fmt"
 
func main() {
    sum := 0
    for i := 1; i <= 10; i++ {
        sum += i
    }
    fmt.Println("累加和为:", sum)
}

In this example, 1 to 10 is calculated through a for loop The numbers are added up and the result is output.

Jump statement

Jump statement is used to change the execution path of the program flow. In Golang, commonly used jump statements include break, continue and goto.

  • The break statement is used to jump out of the current loop immediately:

    for i := 1; i <= 10; i++ {
      if i == 5 {
          break
      }
      fmt.Println(i)
    }

    In this example, when i equals 5, the loop will be jumped out immediately.

  • Thecontinue statement is used to skip the remaining code of the current loop and enter the next loop:

    for i := 1; i <= 10; i++ {
      if i % 2 == 0 {
          continue
      }
      fmt.Println(i)
    }

    In this example, when i is an even number, it will be skipped The code below directly enters the next loop.

  • The goto statement is used to unconditionally jump to the location of the label:

    for i := 1; i <= 10; i++ {
      if i == 5 {
          goto end
      }
      fmt.Println(i)
    }
    end:
    fmt.Println("循环结束")

    In this example, when i equals 5, it will unconditionally jump to The location of the label "end".

The above are the basic usage and code examples of flow control statements in Golang. After mastering these statements, we can better control the logic of the program and achieve different functions and effects. I hope this article will help you understand process control in Golang.

The above is the detailed content of Master the use of flow control statements in Golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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