Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- string
- Class
- C++
- baekjoon
- Deep Learning
- 포인터
- 반복문
- Pre-processing
- vscode
- pass by reference
- array
- 알고리즘
- 티스토리챌린지
- function
- predictive analysis
- programming
- 배열
- 백준
- 파이썬
- const
- 오블완
- 문자열
- Object Oriented Programming
- OOP
- Python
- raw data
- 함수
- assignment operator
- pointer
- Data Science
Archives
- Today
- Total
Channi Studies
[C++] do-while Loop 본문
do-while Loop의 기본적인 형태는 다음과 같습니다.
// do-while Loop
do {
statements;
} while (expression);
do-while문은 while문에서 처럼 conditional expression이 충족되었을 때, do 내부의 statements를 실행합니다.
여기서 중요한 점은, 반복문을 실행한 이후에 조건문을 확인한다는 점입니다.
다시 말하자면, 위의 예시에서 statements부분을 실행한 뒤, expression 부분을 확인합니다.
그렇기 때문에 do-while문의 statements들은 최소 한번의 실행이 보장되어 있습니다.
예시 코드
Example 1
// Example 1
#include <iostream>
using namespace std;
int main(){
int number {};
do {
cout << "Enter an integer between 1 and 5: ";
cin >> number;
} while (number <= 1 || number >= 5);
cout << "Thanks" << endl;
return 0;
}

1차적으로 do { } 내부의 출력과 number 변수에 대한 입력이 행해진 뒤,
while 문 내부의 number에 대한 참거짓 판단이 진행됩니다.
Example 2
// Example 2
#include <iostream>
using namespace std;
int main(){
char selection {};
double height{}, width{};
do {
cout << "Enter width and height separated by a space: ";
cin >> height >> width;
double area (height * width);
cout << "The area is: " << area << "." << endl;
cout << "\nCalculate another? (Y/N) : ";
cin >> selection;
} while (selection == 'Y' || selection == 'y');
cout << "\nCalculation completed." << endl;
return 0;
}

'C++ > 반복문 (Loop)' 카테고리의 다른 글
[C++] While Loop (0) | 2023.12.10 |
---|---|
[C++] Range-based for Loop (0) | 2023.12.10 |
[C++] Conditional Operator (조건 연산자) (2) | 2023.12.08 |