Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the forms of for in go language

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the form of for in go language, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

For is the only control statement that traverses through other loops in Go. For is also more flexible:

Import "fmt" func main () {I: = 1 for {ionization + if I > 3 {break} fmt.Printf (a)} fmt.Printf ("OVER") # # 2package mainimport "fmt" func main () {/ / the most basic one Single conditional loop / / this can replace the while loop of other languages I: = 1 for I 3 {goto LABEL1}} LABEL1: fmt.Printf ("the endless loop is over.\ n")} package mainimport "fmt" func main () {for I: = 1 I < 10; iTunes + {for {goto LABEL1}} LABEL1: fmt.Printf ("endless cycle ends.\ n")}

Egg:continue:

Package mainimport "fmt" func main () {LABEL1: for I: = 0; I < 10; iTunes + {for {continue LABEL1 / / an infinite loop in the inner layer, which controls}} fmt.Printf ("the endless cycle is over.\ n") through the end of a finite loop in the outer layer.

* *

If the conditional statement in the loop is never false, there will be an infinite loop. We can execute the infinite loop by setting only one conditional expression in the for loop statement:

Package main

Import "fmt"

Func main () {

For {/ / you can write true or omit here

Fmt.Printf ("this is an endless cycle.\ n")

}

}

In the for loop, executing the continue statement triggers the execution of the for incremental statement

The goto statement of the Go language can be transferred unconditionally to the line specified in the procedure.

Goto statements are usually used in conjunction with conditional statements. It can be used to realize the function of conditional transfer, forming a cycle, jumping out of the loop body and so on.

However, the use of Goto statements is generally not advocated in structured programming, so as to avoid confusion in the process of the program and difficulties in understanding and debugging the program.

Package mainimport "fmt" func main () {/ * define the local variable * / var an int = 10 / * Loop * / LOOP: for a < 20 {if a = = 15 {/ * Skip iteration * / a = a + 1 goto LOOP} fmt.Printf (the value of "an is:% d\ n" A) thank you for reading this article carefully. I hope the article "what are the forms of for in go language" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report