Continue 也是一个循环控制语句,就像break 语句一样。continue语句与 break语句相反,它不是终止循环,而是强制执行循环的下一次迭代。
顾名思义, continue 语句强制循环继续或执行下一次迭代。当在循环中执行 continue 语句时,将跳过 continue 语句之后的循环内的代码,并开始循环的下一次迭代。
语法:
示例:
考虑您需要编写一个打印从 1 到 10 而不是 6 的数字的程序的情况。指定您必须使用循环来执行此操作,并且只允许使用一个循环。
这是 continue 语句的用法。我们可以在这里做的是,我们可以运行一个从 1 到 10 的循环,并且每次我们必须将迭代器的值与 6 进行比较。如果它等于 6,我们将使用continue语句继续下一次迭代,否则不会打印任何内容我们将打印该值。
下面是上述思想的实现:
输出:
continue;
// C program to explain the use
// of continue statement
#include <stdio.h>
int main() {
// loop from 1 to 10
for (int i = 1; i <= 10; i++) {
// If i is equals to 6,
// continue to next iteration
// without printing
if (i == 6)
continue;
else
// otherwise print the value of i
printf("%d ", i);
}
return 0;
}
1 2 3 4 5 7 8 9 10
continue语句可以与任何其他循环一起使用,也可以与上面的 for 循环一起使用,例如 while 或 do while 。
练习题:
给定一个数字 n,打印三角形图案。我们只能使用一个循环。
Input: 7 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * *