Go的continue语句

continue语句用于跳过循环的剩余部分,然后在检查条件后继续下一次循环迭代。

语法:

continue;  

或者我们也可以这样写:

x:  
continue:x  

Go Continue语句示例:

package main  
import "fmt"  
func main() {  
   /* 本地变量定义*/  
   var a int = 1  
   /* 执行循环*/  
   for a < 10 {  
      if a == 5 {  
         /* 跳过迭代 */  
         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() {  
   /* 本地变量定义 */  
   var a int = 1  
   var b int = 1  
   /* 执行循环 */  
   for a = 1; a < 3; a++ {  
      for b = 1; b < 3; b++ {  
         if a == 2 && b == 2 {  
            /* 跳过迭代 */  
            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

标签: Golang, Golang下载, Golang教程, Golang技术, Golang学习, Golang学习教程, Golang语言, Golang开发, Golang入门教程, Golang进阶教程, Golang高级教程, Golang面试题, Golang笔试题, Golang编程思想