Go continue语句

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

句法:-

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

继续也可以应用于内循环

转到带有内部循环示例的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
微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?