C# continue 语句用于继续循环。它继续程序的当前流程并在指定条件下跳过剩余的代码。在内部循环的情况下,它只继续内部循环。
jump-statement; continue;
using System;
public class ContinueExample
{
public static void Main(string[] args)
{
for(int i=1;i<=10;i++){
if(i==5){
continue;
}
Console.WriteLine(i);
}
}
}
输出:
1 2 3 4 6 7 8 9 10
只有在内循环中使用 continue 语句时,C# Continue 语句才会继续内循环。
using System;
public class ContinueExample
{
public static void Main(string[] args)
{
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++){
if(i==2&&j==2){
continue;
}
Console.WriteLine(i+" "+j);
}
}
}
}
输出:
1 1 1 2 1 3 2 1 2 3 3 1 3 2 3 3