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

Introduction to the usage of cyclic sentences in Go language

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

Share

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

The main content of this article is "introduction to the usage of circular sentences in Go language". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "introduction to the use of Go language loop sentences".

Catalogue

Loop statement

Method one

Method two

Method three

Break keyword

Continue keyword

Goto keyword

Loop statement

Good use of loop statements can help us improve the simplicity and efficiency of the code.

Method one

Format:

For init; condition; post {/ * Loop body * /}

Int: an assignment expression that assigns values to control variables

Condition: relational or logical expressions, loop control conditions

Post: an assignment expression that increments or decrements a control variable

Example:

Package mainimport "fmt" func main () {/ / method-var times = 10 var i int for i = 0; I

< times; i++ { fmt.Println(i) } } 输出结果: 0 1 2 3 4 5 6 7 8 9 方法二 格式: for condition () condition: 关系表达式或逻辑表达式, 循环控制条件 例子: package mainimport "fmt"func main() { // 定义变量 var a = 10 var b = 20 // 方式二 for a < b { fmt.Println("a:", a, " b:", b) a++ }} 输出结果: a: 10 b: 20 a: 11 b: 20 a: 12 b: 20 a: 13 b: 20 a: 14 b: 20 a: 15 b: 20 a: 16 b: 20 a: 17 b: 20 a: 18 b: 20 a: 19 b: 20 方法三 格式: for () 例子: package mainimport "fmt"func main() { // 定义数组 var num = [5]int{1, 2, 3, 4, 5} // 方法三 for i, number := range num { fmt.Println("index:", i, "value:", number) } } 输出结果: index: 0 value: 1 index: 1 value: 2 index: 2 value: 3 index: 3 value: 4 index: 4 value: 5 break 关键字 当我们使用 break 关键字的时候, 语句会直接退出循环, 忽略循环体中任何其他语句和循环条件测试. 例子: package mainimport "fmt"func main() { // 循环 for i := 0; i < 10; i++ { // 调试输出 fmt.Println(i) // 如果i大于5, 跳出循环 if i >

5 {break}

Output result:

0

one

two

three

four

five

six

Continue keyword

The continue keyword is used to end the current iteration in the for loop and then proceed to the next iteration.

Example:

Package mainimport "fmt" func main () {/ / Loop for I: = 0; I < 10; if I equals 5, skip if I = = 5 {continue} / / debug output fmt.Println (I)}}

Output result:

0

one

two

three

four

six

seven

eight

nine

We can see that 5 has not been printed.

Goto keyword

The goto keyword can help us transfer control to the marked statement.

Example:

Package mainimport "fmt" func main () {/ / define variable var I = 0 LOOP: for I < 10 {if I = = 5 {if + / / Skip goto LOOP} / / debug output fmt.Println (I) iTunes +}}

Output result:

0

one

two

three

four

six

seven

eight

nine

We can see that 5 has not been printed.

At this point, I believe you have a deeper understanding of the "introduction to the use of circular sentences in Go language". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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