1. Loop sentences
1. Ordinary cycle
1) Syntax
for init; condition; post { }
- init (optional): Assign initial value to the control variable;
- condition (optional): control condition (if not filled in, it is equal to
while True
); - post (optional): controls the increment or decrement of variables;
2) Give an example
1. Find the sum of numbers from 1 to 10.
package main import "fmt" func main() { count := 0 for i := 0; i <= 10; i++ { count += i } ("The sum of numbers from 1 to 10 is:",count) }
Execution results
The sum of numbers from 1 to 10 is: 55
2. Omit init and post: Calculate the value of self-added when count is less than 10:
package main import "fmt" func main() { count := 1 for ; count < 10; { count += count } ("The value of self-added when count is less than 10 is:",count) }
Can also be omitted;
Number:
package main import "fmt" func main() { count := 1 for count < 10{ //Omit the semicolon count += count } ("The sum of numbers from 1 to 10 is:",count) }
Execution results
When count is less than 10, the value of self-added is
There is no while loop in it, so it can be achieved by omitting the condition:
package main import "fmt" func main() { for { //Omit condition ("Deadly cycle") } }
2. Loop nesting
package main import "fmt" func main() { for { //Omit condition ("Deadly cycle") } }
Output result
(Sum of numbers from 1 to 10)x10: 550
3. Range loop
Used to iterate the output elements on strings, arrays, slices, etc.:
package main import "fmt" func main() { strArray := []string{"a", "b","c"} //Array of strings for i,v := range strArray { (("Subscript is: %d value is: %s", i,v)) } }
Output result
Subscript is: 0 Value is: a
Subscript is: 1 Value is: b
Subscript is: 2 Value is: c
2. Loop control statement
- Interrupt (jump out) loop
1) Interrupt (jump out) loop
Write a vicious loop, variablea
1 will be added all the time, and when the value of a is greater than 3, the loop will be jumped out:
package main import "fmt" func main() { a := 1 for { a++ ("The value of a is : %d\n", a) if a > 3 { /* Use break statement to break out of loop */ ("Breaking out of the loop") break } } }
Output result
The value of a is: 2
The value of a is: 3
The value of a is: 4
Break out of the loop
2) Specify the loop you want to interrupt (bounce out) (used in nested loops)
Use labels to specify the loop you want to jump out.
The following is the unused mark. Normal break interrupts the loop, which will only interrupt the current layer loop and will not interrupt the outer layer. The value printed by the outer layer is always 11:
package main import "fmt" func main() { for i := 1; i <= 2; i++ { ("Outer loop i: %d\n", i) for j := 11; j <= 12; j++ { ("Inner loop j: %d\n", j) break //Not using marks, the loop of this layer will only be interrupted, and the outer loop will not be interrupted } } }
Output result
Outer loop i: 1
Inner loop j: 11
Outer loop i: 2
Inner loop j: 11
The following is the use of a flag, specifying that the outer loop is interrupted, which means that the loop is executed only once:
package main import "fmt" func main() { re: for i := 1; i <= 2; i++ { ("Outer loop i: %d\n", i) for j := 11; j <= 12; j++ { ("Inner loop j: %d\n", j) break re //Use markers to interrupt the outer loop } } }
Output result
Outer loop i: 1
Inner loop j: 11
-Skip this loop
continue is to skip the loop that is executed after the loop, rather than interrupting the loop
package main import "fmt" func main() { for a := 1; a < 5; a++ { if a == 3 { //Skip the execution of continue when a=3 continue } //When a=3, the printing operation will not be performed ("The value of a is : %d\n", a) } }
Execution results
The value of a is: 1
The value of a is: 2
The value of a is: 4
When loop nesting, continue can also specify skipped loops, and the usage is the same as break
- Conditional Transfer
goto can be directly transferred to the specified code for execution.
The following code, when a=3, will jump out of the for loop and directly execute the code on the line where the LOOP is located:
package main import "fmt" func main() { for a := 1; a < 5; a++ { if a == 3 { //a equals 3; execute goto jump out goto LOOP } //When a=3, the printing operation will not be performed ("The value of a is : %d\n", a) } LOOP:("a equals 3; execute goto jump out!") }
Execution results
The value of a is: 1
The value of a is: 2
a is equal to 3; execute goto to jump out!
It is not recommended to use goto, which can easily cause confusion in the code structure.
This is the article about Golang's loop statements and loop control statements. For more relevant go loop statements and loop control statements, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!