📜  GO语言 continue

📅  最后修改于: 2021-01-02 09:07:15             🧑  作者: Mango

继续声明

继续用于跳过循环的其余部分,然后在检查条件之后继续循环的下一个迭代。

句法:-

continue;

或者我们可以喜欢

 x:
    continue:x

继续执行语句示例:

package main
import "fmt"
func main() {
   /* local variable definition */
   var a int = 1
   /* do loop execution */
   for a < 10 {
      if a == 5 {
         /* skip the iteration */
         a = a + 1;
         continue;
      }
      fmt.Printf("value of a: %d\n", a);
      a++;
   }
}

输出:

value of a: 1
value of a: 2
value of a: 3
value of a: 4
value of a: 6
value of a: 7
value of a: 8
value of a: 9

继续也可以应用于内循环

转到带有内部循环的继续语句示例:

package main
import "fmt"
func main() {
   /* local variable definition */
   var a int = 1
   var b int = 1
   /* do loop execution */
   for a = 1; a < 3; a++ {
      for b = 1; b < 3; b++ {
         if a == 2 && b == 2 {
            /* skip the iteration */
            continue;
         }
         fmt.Printf("value of a and b is %d %d\n", a, b);
      }
      fmt.Printf("value of a and b is %d %d\n", a, b);
   }
}

输出:

value of a and b is 1 1
value of a and b is 1 2
value of a and b is 1 3
value of a and b is 2 1
value of a and b is 2 3