Skip to content
On this page

循环结构之 continue

循环中可以应用 continue 关键字中止一次循环,直接进入下一次。请看下面的例子:

img

当程序执行到到 continue; 的时候,会立即停止本次循环体,直接进入下一次循环。所以,第三行输出比其他行少一些:

img

所以,可以使用 continue 关键字,在循环中剔除一些特殊的数据。

任务

右边的代码循环输出 1-9 的整数,请使用 continue 关键字,使得 3 和 8 不会被打印。

代码

csharp
using System;
using System.Collections.Generic;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int x = 1; x < 10; x++)  
            {
                //请添加代码,过滤 3 和 8
                if (x == 3 || x == 8)
                    continue;
                Console.Write(x);
            }
        }
    }
}