C++의 While 루프 루프의 정확한 반복 횟수를 미리 알 수 없는 상황에서 사용됩니다. 테스트 조건에 따라 루프 실행이 종료됩니다. C++의 루프 명령문 블록을 반복적으로 실행해야 할 때 사용됩니다. 연구하는 동안 C++의 'for' 루프 , 우리는 반복 횟수가 미리 알려져 있음을 보았습니다. 즉, 루프 본문을 실행하는 데 필요한 횟수가 우리에게 알려져 있습니다.

통사론:
while (test_expression) { // statements update_expression; }> 다양한 While 루프의 일부 이다:
- 테스트 표현식: 이 표현식에서는 조건을 테스트해야 합니다. 조건이 true로 평가되면 루프 본문을 실행하고 표현식 업데이트로 이동합니다. 그렇지 않으면 while 루프를 종료합니다. 업데이트 표현식: 루프 본문을 실행한 후 이 표현식은 루프 변수를 일부 값만큼 증가/감소시킵니다. 본문: 변수, 함수 등을 포함하는 명령문 그룹입니다. while 루프를 사용하면 코드 및 간단한 이름을 인쇄할 수 있고, 복잡한 알고리즘을 실행하거나 기능적 작업을 수행할 수 있습니다.
While 루프는 어떻게 실행되나요?
- 제어는 while 루프에 속합니다.
- 흐름이 조건으로 점프합니다.
- 상태가 테스트되었습니다.
- Condition이 true를 산출하면 흐름이 Body로 들어갑니다.
- 조건이 거짓을 산출하면 흐름은 루프 밖으로 나갑니다.
- 루프 본문 내부의 명령문이 실행됩니다.
- 업데이트가 이루어집니다.
- 제어 흐름은 2단계로 다시 돌아갑니다.
- while 루프가 종료되고 흐름이 외부로 나갔습니다.
while 루프의 흐름도
예시 1: 이 프로그램은 몇 가지 조건에 따라 Hello World를 5번 인쇄하려고 시도합니다.
C++
배쉬 변수
// C++ program to illustrate while loop> > #include> using> namespace> std;> > int> main()> {> >// initialization expression> >int> i = 1;> > >// test expression> >while> (i <6) {> >cout <<>'Hello World
'>;> > >// update expression> >i++;> >}> > >return> 0;> }> |
자바 기초
>
>산출:
Hello World Hello World Hello World Hello World Hello World>
실시예 1의 드라이런:
1. Program starts. 2. i is initialized with value 1. 3. Condition is checked. 1 <6 yields true. 3.a) 'Hello World' gets printed 1st time. 3.b) Updation is done. Now i = 2. 4. Condition is checked. 2 < 6 yields true. 4.a) 'Hello World' gets printed 2nd time. 4.b) Updation is done. Now i = 3. 5. Condition is checked. 3 < 6 yields true. 5.a) 'Hello World' gets printed 3rd time 5.b) Updation is done. Now i = 4. 6. Condition is checked. 4 < 6 yields true. 6.a) 'Hello World' gets printed 4th time 6.b) Updation is done. Now i = 5. 7. Condition is checked. 5 < 6 yields true. 7.a) 'Hello World' gets printed 5th time 7.b) Updation is done. Now i = 6. 8. Condition is checked. 6 < 6 yields false. 9. Flow goes outside the loop to return 0.>
예시 2:
C++
자바의 목록 예
자바스크립트 base64 디코드
// C++ program to illustrate while loop> > #include> using> namespace> std;> > int> main()> {> >// initialization expression> >int> i = 1;> > >// test expression> >while> (i>-5) {> >cout << i <<>'
'>;> > >// update expression> >i--;> >}> > >return> 0;> }> |
>
>산출:
1 0 -1 -2 -3 -4>