2. continue 语句

continue 语句用于迭代语句(for 语句、while 语句和 do-while 语句)中,当continue 语句执行后则不再执行本次循环内 continue 之后的语句,重新开始一次新的循环。

语法

continue;

语法中:continue 是关键字。

语法说明:

以下我们来用示意性的代码来说明 continue 语句在各个语句中的跳转位置。

一、continue 在 for 语句中执行后的跳转位置。

for (/* ... */) {
    /* ... */
    continue;
    /* ... */
    // <--- continue 跳转到此处
}

二、continue 在 while 语句中执行后的跳转位置。

...
while (/* ... */ )
{
    /* ... */
    continue;
    /* ... */
    // <--- continue 跳转到此处
}

三、continue 在 do-while 语句中执行后的跳转位置。

...
do {
    /* ... */
    continue;
    /* ... */
    // <--- continue 跳转到此处
} while(/* ... */);

示例:

编写一个程序,输出 1 到 20 之间的所有数字,但跳过所有能被 3 和 7 整除的数字。

#include <stdio.h>

int main(int argc, char *argv[]) {
    for (int i = 1; i <= 20; i++) {
        if (i % 7 == 0)
            continue;
        if (i % 3 == 0)
            continue;
        printf("%d\n", i);
    }
    return 0;
}

练习:

写一个程序 ,打印 1~100 范围内个位、十位、百位都不包含 7,且不能被 7 整除的所有整数。