亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> C语言

C++ 继续语句

C++ continue 语句

在本教程中,我们将通过示例了解 continue 语句及其与循环的关系。

在计算机编程中,continue 语句用于跳过循环的当前迭代,程序的控制转到下一次迭代。

continue 的语法 声明是:

continue;

在了解 continue 语句之前,请确保您了解,


C++ continue 语句的工作

<图>

例1:继续for循环

for 循环,continue 跳过当前迭代,控制流跳转到update 表达。

// program to print the value of i

#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 5; i++) {
        // condition to continue
        if (i == 3) {
            continue;
        }

        cout << i << endl;
    }

    return 0;
}

输出

1
2
4
5

在上面的程序中,我们使用了 for 循环打印 i 的值 在每次迭代中。这里,注意代码,

if (i == 3) {
    continue;
}

这意味着

注意 :continue 语句几乎总是与决策语句一起使用。


示例 2:使用 while 循环继续

while 循环,continue 跳过程序的当前迭代和控制流跳回while condition .

// program to calculate positive numbers till 50 only
// if the user enters a negative number,
// that number is skipped from the calculation

// negative number -> loop terminate
// numbers above 50 -> skip iteration

#include <iostream>
using namespace std;

int main() {
    int sum = 0;
    int number = 0;

    while (number >= 0) {
        // add all positive numbers
        sum += number;

        // take input from the user
        cout << "Enter a number: ";
        cin >> number;

        // continue condition
        if (number > 50) {
            cout << "The number is greater than 50 and won't be calculated." << endl;
            number = 0;  // the value of number is made 0 again
            continue;
        }
    }

    // display the sum
    cout << "The sum is " << sum << endl;

    return 0;
}

输出

Enter a number: 12
Enter a number: 0
Enter a number: 2
Enter a number: 30
Enter a number: 50
Enter a number: 56
The number is greater than 50 and won't be calculated.
Enter a number: 5
Enter a number: -3
The sum is 99 

在上述程序中,用户输入一个数字。 while 循环用于打印用户输入的正数的总和,只要输入的数字不大于50 .

注意 continue 的使用 声明。

 if (number > 50){
    continue;
}

注意 :continue do...while 语句的工作方式相同 循环。


继续嵌套循环

continue 与嵌套循环一起使用时,它会跳过内部循环的当前迭代。例如,

// using continue statement inside
// nested for loop

#include <iostream>
using namespace std;

int main() {
    int number;
    int sum = 0;

    // nested for loops

    // first loop
    for (int i = 1; i <= 3; i++) {
        // second loop
        for (int j = 1; j <= 3; j++) {
            if (j == 2) {
                continue;
            }
            cout << "i = " << i << ", j = " << j << endl;
        }
    }

    return 0;
}

输出

i = 1, j = 1
i = 1, j = 3
i = 2, j = 1
i = 2, j = 3
i = 3, j = 1
i = 3, j = 3

在上面的程序中,当continue 语句执行时,它会跳过内部循环中的当前迭代。并且程序的控制转移到更新表达式 内循环。

因此, j =2 的值 永远不会显示在输出中。

注意 :break 语句完全终止循环。但是,continue 语句只跳过当前迭代。


C语言

  1. C# if, if...else, if...else if 和嵌套 if 语句
  2. C# switch 语句
  3. C# 中断语句
  4. C# continue 语句
  5. C++ 类型转换
  6. C++ 运算符
  7. C++ 注释
  8. C++ if, if...else 和嵌套 if...else
  9. C++ while 和 do...while 循环
  10. C++ 中断语句
  11. C++ 类模板
  12. 带有示例的 C++ Switch Case 语句