Cprogramming 简明教程
Continue Statement in C
C 中 continue 语句的行为与 break statement 有点相反。它不是强制终止循环,而是强制执行循环的下一次迭代,跳过当前迭代中的其余语句。
The behaviour of continue statement in C is somewhat opposite to the break statement. Instead of forcing the termination of a loop, it forces the next iteration of the loop to take place, skipping the rest of the statements in the current iteration.
What is Continue Statement in C?
continue 语句用于跳过在当前迭代中循环中其余语句的执行并将其转移到下一个循环迭代。它可以与所有 C language loop constructs ( while 、 do while 和 for )一起使用。
The continue statement is used to skip the execution of the rest of the statement within the loop in the current iteration and transfer it to the next loop iteration. It can be used with all the C language loop constructs (while, do while, and for).
Continue Statement Syntax
continue 语句按照以下结构使用 −
The continue statement is used as per the following structure −
while (expr){
. . .
. . .
if (condition)
continue;
. . .
}
Continue Statement Flowchart
以下 flowchart 表示 continue 的工作原理 −
The following flowchart represents how continue works −
您必须在循环中使用 continue 语句。如果您在循环外部使用 continue 语句,则会导致编译错误。与 break 语句不同, continue 不与 switch-case statement 一起使用。
You must use the continue statement inside a loop. If you use a continue statement outside a loop, then it will result in compilation error. Unlike the break statement, continue is not used with the switch-case statement.
Continue Statement with Nested Loops
在 nested loops 的情况下, continue 将继续最近循环的下一个迭代。 continue 语句通常与 if statements 一起使用。
In case of nested loops, continue will continue the next iteration of the nearest loop. The continue statement is often used with if statements.
Continue Statement Examples
Example: Continue Statement with While Loop
在此程序中,该循环生成 variable "i" 的 1 到 10 个值。每当它是一个偶数时,下一次迭代就开始,跳过 printf 语句。只有奇数被打印出来。
In this program the loop generates 1 to 10 values of the variable "i". Whenever it is an even number, the next iteration starts, skipping the printf statement. Only the odd numbers are printed.
#include <stdio.h>
int main(){
int i = 0;
while (i < 10){
i++;
if(i%2 == 0)
continue;
printf("i: %d\n", i);
}
}
i: 1
i: 3
i: 5
i: 7
i: 9
Example: Continue Statement with For Loop
以下程序过滤出 string 中的所有元音 −
The following program filters out all the vowels in a string −
#include <stdio.h>
#include <string.h>
int main () {
char string[] = "Welcome to TutorialsPoint C Tutorial";
int len = strlen(string);
int i;
printf("Given string: %s\n", string);
printf("after removing the vowels\n");
for (i=0; i<len; i++){
if (string[i]=='a' || string[i]=='e' || string[i] == 'i' || string[i] == 'o' || string[i] == 'u')
continue;
printf("%c", string[i]);
}
return 0;
}
运行代码并检查其输出:
Run the code and check its output −
Given string: Welcome to TutorialsPoint C Tutorial
after removing the vowels
Wlcm t TtrlsPnt C Ttrl
Example: Continue Statement with Nested Loops
如果 continue 语句出现在内部循环中,则程序控制跳转到相应循环的开头。
If a continue statement appears inside an inner loop, the program control jumps to the beginning of the corresponding loop.
在以下示例中,有三个 for 循环,一个套一个。这些循环分别由变量 i 、 j 和 k 控制。如果 k 等于 i 或 j ,则最内层循环跳过 printf 语句并转到 k 的下一个值。第二个 j 循环在它等于 i 时执行 continue 。因此,三个数字 1、2 和 3 的所有唯一组合都将显示出来。
In the example below, there are three for loops one inside the other. These loops are controlled by the variables i, j, and k respectively. The innermost loop skips the printf statement if k is equal to either i or j, and goes to its next value of k. The second j loop executes the continue when it equals i. As a result, all the unique combinations of three digits 1, 2 and 3 are displayed.
#include <stdio.h>
int main (){
int i, j, k;
for(i = 1; i <= 3; i++){
for(j = 1; j <= 3; j++){
if (i == j)
continue;
for (k=1; k <= 3; k++){
if (k == j || k == i)
continue;
printf("%d %d %d \n", i,j,k);
}
}
}
return 0;
}
运行代码并检查其输出:
Run the code and check its output −
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
Example: Removing Spaces Between Words in a String
以下代码检测字符串中单词之间的空白,并将每个单词打印到不同的行上。
The following code detects the blankspaces between the words in a string, and prints each word on a different line.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main(){
char string[] = "Welcome to TutorialsPoint C Tutorial";
int len = strlen(string);
int i;
printf("Given string: %s\n", string);
for (i = 0; i < len; i++){
if (string[i] == ' '){
printf("\n");
continue;
}
printf("%c", string[i]);
}
return 0;
}
执行此代码后,您将获得以下输出 −
On executing this code, you will get the following output −
Given string: Welcome to TutorialsPoint C Tutorial
Welcome
to
TutorialsPoint
C
Tutorial
Example: Finding Prime Factors of a Number
使用 continue 语句非常有效的情况之一,就是编写一个程序来查找 prime factors of a given number 的问题。
One of the cases where the continue statement proves very effective is in the problem of writing a program to find prime factors of a given number.
本程序的 algorithm 的工作原理如下 −
The algorithm of this program works like this −
给定的数字依次除以从 2 开始的数字。如果数字可被整除,则给定的数字将被缩小为除法,并且所得出的数字将被检查与 2 的可整除性,直到不再可整除。
The given number is successively divided by numbers starting with 2. If the number is divisible, the given number is reduced to the division, and the resultant number is checked for divisibility with 2 until it is no longer divisible.
如果不除以 2,则对从 3 开始的所有奇数重复此过程。循环将持续到给定的数字缩减到 1。
If not by 2, the process is repeated for all the odd numbers starting with 3. The loop runs while the given number reduces to 1.
以下是查找质因数的程序 −
Here’s the program to find the prime factors −
#include <stdio.h>
int main (){
int n = 64;
int i, m = 2;
printf("Prime factors of %d: \n", n);
while (n > 1){
if (n % m == 0){
n = n/m;
printf("%d ", m);
continue;
}
if (m == 2)
m++;
else
m = m+2;
}
return 0;
}
此处,给定的数字为 64。因此,当您运行此代码时,它将产生以下输出 −
Here, the given number is 64. So, when you run this code, it will produce the following output −
Prime factors of 64:
2 2 2 2 2 2
将数字更改为 45,再更改为 90。再次运行代码。现在您将获得以下输出 −
Change the number to 45 and then 90. Run the code again. Now you will get the following outputs −
Prime factors of 45:
3 3 5
Prime factors of 90:
2 3 3 5