산업 제조
산업용 사물 인터넷 | 산업자재 | 장비 유지 보수 및 수리 | 산업 프로그래밍 |
home  MfgRobots >> 산업 제조 >  >> Industrial programming >> C 언어

C++ 계속 문

C++ 계속 문

이 자습서에서는 예제를 통해 계속 문과 루프 작업에 대해 배웁니다.

컴퓨터 프로그래밍에서 continue 문은 루프의 현재 반복을 건너뛰는 데 사용되며 프로그램의 제어는 다음 반복으로 이동합니다.

continue 구문 성명:

continue;

continue 문에 대해 알아보기 전에 다음 사항을 확인하십시오.

<시간>

C++ 작업 계속 문

<그림> <시간>

예제 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# break 문
  4. C# 계속 문
  5. C++ 유형 변환
  6. C++ 연산자
  7. C++ 주석
  8. C++ if, if...else 및 중첩 if...else
  9. C++ 클래스 템플릿
  10. EXAMPLE이 있는 C++ Switch Case 문