This is the fourth day of my participation in the August More text Challenge. For details, see:August is more challenging

A loop statement means that a condition can be met to execute a piece of code repeatedly. For is the only loop statement. (Go has no while loop)

For statement

Grammatical structure:

for init; condition; post { }
Copy the code

The initialization statement is executed only once. This condition is checked after the initialization of the loop. If the condition evaluates to true, the body of the loop in {} is executed, followed by the POST statement. The POST statement is executed after each successful iteration of the loop. After the POST statement is executed, the condition is rechecked. If it is correct, the loop continues, otherwise the loop terminates.

  • The initialization * statement is optional and should be executed before the loop begins. Initialization statements are always in simple statements, such as variable declarations, increment or assignment statements, or function calls.
  • The condition statement contains a Boolean expression that evaluates at the beginning of each iteration of the loop. If the value of the conditional statement is true, the loop is executed.
  • The POST statement executes after the body of the for loop. After the POST statement, the conditional statement evaluates again whether the value of the conditional statement is false, and the loop ends.

Example code:

package main

import (  
    "fmt"
)

func main(a) {  
    for i := 1; i <= 10; i++ {
        fmt.Printf("%d",i)
    }
}
Copy the code

Variables declared in a for loop are only available within the scope of the loop. Therefore, I cannot access the loop externally.

For loop variant

All three components, initialization, conditions, and POST, are optional.

for condition { } // The effect is similar to while
Copy the code
for{}For (;;); The same
Copy the code

The range format of the for loop iterates through slice, map, array, string, and so on

for key, value := range oldMap {
    newMap[key] = value
}

package main

import "fmt"

func main(a) {

   var b int = 15
   var a int

   numbers := [6]int{1.2.3.5} 

   /* for loop */
   for a := 0; a < 10; a++ {
      fmt.Printf("Value of a: %d\n", a)
   }

   for a < b {
      a++
      fmt.Printf("Value of a: %d\n", a)
      }

   for i,x:= range numbers {
      fmt.Printf("Value of the %d bit x = %d\n", i,x)
   }   
}
Copy the code
  • Key and value are the variables that assign the iteration value. They are also called iteration variables.
  • The second variable, value, is optional.
  • The range expression is evaluated once before the loop begins.

Running result:

The value of a is:0The value of a is:1The value of a is:2The value of a is:3The value of a is:4The value of a is:5The value of a is:6The value of a is:7The value of a is:8The value of a is:9The value of a is:1The value of a is:2The value of a is:3The value of a is:4The value of a is:5The value of a is:6The value of a is:7The value of a is:8The value of a is:9The value of a is:10The value of a is:11The value of a is:12The value of a is:13The value of a is:14The value of a is:150The value of bit x is equal to11The value of bit x is equal to22The value of bit x is equal to33The value of bit x is equal to54The value of bit x is equal to05The value of bit x is equal to0
Copy the code

Use a for loop on a string: the for loop iterates through the Unicode code points of the string.

Grammar:

for index, chr:= range str{
     / / statement..
}
Copy the code

Here, the index is the variable that stores the first byte of the UTF-8 encoding code point, while CHR is the variable that stores the characters of a given string, and STR is the string.

The sample

package main

import "fmt"

func main(a) {

    // The string is used as the scope in the for loop
    for i, j := range "XabCd" {
        fmt.Printf("%U has index value %d\n", j, i)
    }

}
Copy the code

Output:

U+0058The index of is0
U+0061The index of is1
U+0062The index of is2
U+0043The index of is3
U+0064The index of is4
Copy the code

Jump out of the loop

Break statement

Break: Break out of the loop body. The break statement is used to abruptly terminate a for loop before ending its normal execution

Example code:

package main

import (  
    "fmt"
)

func main(a) {  
    for i := 1; i <= 10; i++ {
        if i > 5 {
            break //loop is terminated if i > 5
        }
        fmt.Printf("%d ", i)
    }
    fmt.Printf("\nline after for loop")}Copy the code

The continue statement

Continue: Break out of a loop. The continue statement is used to skip the current iteration of the for loop. All code in the for loop following the continue statement will not be executed in the current iteration. The loop continues to the next iteration.

Example code:

package main

import (  
    "fmt"
)

func main(a) {  
    for i := 1; i <= 10; i++ {
        if i%2= =0 {
            continue
        }
        fmt.Printf("%d ", i)
    }
}
Copy the code

Goto statements

Goto: Can be transferred unconditionally to the row specified in the procedure.

Grammatical structure:

gotolabel; . . label: statement;Copy the code

package main

import "fmt"

func main(a) {
   /* Define local variables */
   var a int = 10

   / * * / cycles
   LOOP: for a < 20 {
      if a == 15 {
         /* Skip iteration */
         a = a + 1
         goto LOOP
      }
      fmt.Printf("Value of a: %d\n", a)
      a++     
   }  
}
Copy the code