This is a creation in Article, where the information may have evolved or changed. This is the 9th part of [Golang Series Tutorial] (/SUBJECT/2). A looping statement is used to repeat a piece of code. ' For ' is the only looping statement in the Go language. There are no other languages in the Go language, such as the ' while ' and ' do ' loops in the C language. # # for loop syntax ' ' Gofor initialisation; Condition The post {} ' initialization statement executes only once. After the loop is initialized, the loop condition is checked. If the condition evaluates to ' true ', the loop body within ' {} ' executes, followed by the post statement. The post statement executes after each successful loop iteration. After the post statement is executed, the condition is checked again. If ' true ', the loop will continue to execute, otherwise the for loop will be terminated. (This is a typical for loop three expressions, the first is an initialization expression or an assignment statement, the second is a cyclic condition to determine the expression, the third is to fix the loop variable expression, that is, the post here) these three components, that is, initialization, conditions and post are optional. Let's look at an example to better understand the loop. The # # example lets us use a ' for ' Loop to write a program that prints out from 1 to 10. "' Gopackage mainimport (" FMT ") func main () {for i: = 1; i <=; i++ {fmt. Printf ("%d", I)} "[Run in Playground] (Https://play.golang.org/p/mV6Zgcx2DY" Run in Playground ") is initialized to 1 in the above program. The conditional statement checks if I is less than 10. If the condition is true, I will be printed out, or the loop will be terminated. The loop statement will increment by 1 after each loop is completed. Once I becomes larger than 10, the loop stops. The above program will print out ' 1 2 3 4 5 6 7 8 9 10 '. Variables declared in the ' for ' loop can only be accessed within the loop body, so I cannot be accessed outside the loop body. The break ' break ' statement is used to abruptly terminate a For loop before it completes normal execution, and then the program will start executing the next line of code in the For loop. Let's write a program that prints from 1 to 5 and uses ' break ' to jump out of the loop. "' Gopackage mainimport ("FMT ") Func main () {for i: = 1; I <=, i++ {if i > 5 {break//loop is terminated if I > 5} fmt. Printf ("%d", I)} fmt. Printf ("\nline")} ' [Run in playground] (https://play.golang.org/p/sujky92f--"Run in Playground") In the above procedure, the value of I will be judged during the loop. If the value of I is greater than 5 and the ' break ' statement executes, the loop is terminated. The print statement executes at the end of the ' for ' Loop, and the program output is "' 1 2 3 4 5 line after for Loop ' # # Continue ' Continue ' statement used to jump out of the current loop in the ' for ' Loop. All the ' for ' loop statements after the ' Continue ' statement are not executed in this loop. The loop continues to execute in the next loop. Let's write a program that prints out 1 to 10 and uses ' Continue '. "' Gopackage mainimport (" FMT ") func main () {for i: = 1; i <=; i++ {if i%2 = 0 {continue} fmt. Printf ("%d", I)} "" [Run in Playground] (Https://play.golang.org/p/DRLN2ZHwVS "Run in Playground") is in the above program, this line of code ' if i%2= =0 ' will determine if the remainder of I divided by 2 is 0, and if it is 0, the number is even and then executes the ' continue ' statement, thus controlling the program into the next loop. So the print statement after ' Continue ' is not called and the program enters a loop. The above program will output ' 1 3 5 7 9 '. # # More examples let's write more code to demonstrate the diversity of the ' for ' Loop. The following program prints all the even numbers from 0 to 10. "' Gopackage mainimport (" FMT ") func main () {i: = 0 for; I <= 10; {//INITialisation and post are omitted FMT. Printf ("%d", i) i + = 2} "" [Run in Playground] (https://play.golang.org/p/PNXliGINku "Run in Playground") as we already know, ' fo The three parts of the R ' Loop, initialization statements, conditional statements, and post statements are optional. In the above program, both the initialization statement and the post statement are omitted. I was initialized to 0 outside of the ' for ' Loop. As long as the ' i<=10 ' loop is executed. In the loop, I increment from 2 increments. The above program will output ' 0 2 4 6 8 10 '. The semicolon in the ' for ' Loop in the above program can also be omitted. The ' for ' loop in this format can be thought of as a ' for while ' Loop with two selected. The above program can be rewritten as: "' Gopackage mainimport (" FMT ") func main () {i: = 0 for i <= {//semicolons is ommitted and only Condi tion is present fmt. Printf ("%d", i) i + = 2}} "[Run in Playground] (https://play.golang.org/p/UYiz-Wtnoj" Run in Playground ") can be declared in the ' for ' Loop and Manipulate multiple variables. Let's write a program that uses declaring multiple variables to print the following sequence. "10 * 1 = 10 11 * 2 = 22 12 * 3 = 36 13 * 4 = 52 14 * 5 = 70 15 * 6 = 90 16 * 7 = 112 17 * 8 = 136 18 * 9 = 162 19 * 10 = "" "Gopackage mainimport (" FMT ") func main () {for No, I: = ten, 1; I <= && no <=; I, no = i+1, no+1 {//multiple initialisation and increment fmt. Printf ("%d *%d =%d\n", no, I, No*i)}}' [Run in playground] (https://play.golang.org/p/e7Pf0UDjj0 "Run in Playground") is declared in the above program ' No ' and ' I ' are then initialized to 10 and 1 respectively. At the end of each cycle ' no ' and ' I ' are increased by 1. The boolean operator ' && ' is used to ensure I is less than or equal to 10 and ' no ' is less than or equal to 19. # # Infinite loop infinite loop syntax is: ' ' gofor {} ' next program will always print ' Hello world ' will not stop. "' Gopackage mainimport" FMT "Func Main () {for {fmt. Println ("Hello World")} "if you intend to try the above program in [Go Playground] (https://play.golang.org/p/kYQZw1AWT4" Go Playground "), You'll get a "process that takes too long" error. Please try running on your local system to print "Hello world" indefinitely. There is also a ' range ' structure that can be used to manipulate array objects in a ' for ' loop. We will supplement this when we learn the array. This is the entire contents of the ' for ' Loop. I hope you can enjoy this reading. Please leave your valuable comments and suggestions. * * Previous Tutorial-[If-else statement] (https://studygolang.com/articles/11902) * * * * Next tutorial-[switch statement] (https://studygolang.com/articles /11957) * *
via:https://golangbot.com/loops/
Author: Nick Coghlan Translator: Thxallvu proofreading: polaris1119
This article by GCTT original compilation, go language Chinese network honor launches
This article was originally translated by GCTT and the Go Language Chinese network. Also want to join the ranks of translators, for open source to do some of their own contribution? Welcome to join Gctt!
Translation work and translations are published only for the purpose of learning and communication, translation work in accordance with the provisions of the CC-BY-NC-SA agreement, if our work has violated your interests, please contact us promptly.
Welcome to the CC-BY-NC-SA agreement, please mark and keep the original/translation link and author/translator information in the text.
The article only represents the author's knowledge and views, if there are different points of view, please line up downstairs to spit groove
3,001 Reads