Home > Backend Development > Golang > What is the use of break stop statement in Go language?

What is the use of break stop statement in Go language?

青灯夜游
Release: 2023-01-28 09:03:49
Original
1584 people have browsed it

In the Go language, the break stop statement is used to jump out of the loop in a loop statement and start executing the statement after the loop. The break statement can end the code blocks of for, switch and select. In addition, the break statement can also add a label after the statement to indicate exiting the code block corresponding to a certain label. The label requirement must be defined on the corresponding code block of for, switch and select. .

What is the use of break stop statement in Go language?

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

When we use a for loop, when certain conditions are met, we need to terminate the loop's continued execution. In the Go language, the keyword used to terminate the loop's continued execution is break.

In the Go language, the break stop statement is used to jump out of the loop in a loop statement and start executing the statement after the loop.

The break statement in Go language can end the code blocks of for, switch and select. In addition, the break statement can also add a label after the statement to indicate exiting the code block corresponding to a certain label. The label requirements must be defined. On the corresponding code blocks of for, switch and select.

  • break is the function of jumping out of the statement after executing a case in switch (switch statement).

  • In multiple loops, you can use label to mark the loop you want to break.

Grammar

break The syntax format is as follows:

break;
Copy after login

break statement flow chart is as follows:

What is the use of break stop statement in Go language?

Example

Break out of the loop when variable a is greater than 15:

package main

import "fmt"

func main() {
   /* 定义局部变量 */
   var a int = 10

   /* for 循环 */
   for a < 20 {
      fmt.Printf("a 的值为 : %d\n", a);
      a++;
      if a > 15 {
         /* 使用 break 语句跳出循环 */
         break;
      }
   }
}
Copy after login

What is the use of break stop statement in Go language?

The following example has multiple loops, demonstrating the difference between using markers and not using markers:

package main

import "fmt"

func main() {

   // 不使用标记
   fmt.Println("---- break ----")
   for i := 1; i <= 3; i++ {
      fmt.Printf("i: %d\n", i)
      for i2 := 11; i2 <= 13; i2++ {
         fmt.Printf("i2: %d\n", i2)
         break
      }
   }

   // 使用标记
   fmt.Println("---- break label ----")
   re:
      for i := 1; i <= 3; i++ {
         fmt.Printf("i: %d\n", i)
         for i2 := 11; i2 <= 13; i2++ {
         fmt.Printf("i2: %d\n", i2)
         break re
      }
   }
}
Copy after login

What is the use of break stop statement in Go language?

[Related recommendations: Go video tutorial, Programming teaching

The above is the detailed content of What is the use of break stop statement 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