GO教程
GO控制语句
GO高级

Go Continue

Go Continue 语句

continue 用于跳过循环的剩余部分,然后在检查条件后继续循环的下一次迭代。
语法:-
continue;
或者我们可以这样做
 x:
continue:x
Go Continue 语句示例:
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
Continue 也可以应用于内循环
Go Continue 语句与内循环示例:
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

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4