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
- 알고리즘
- C++
- 백준
- baekjoon
- 함수
- Pre-processing
- OOP
- assignment operator
- programming
- Python
- Deep Learning
- 문자열
- array
- pass by reference
- 파이썬
- const
- 배열
- predictive analysis
- Class
- 반복문
- pointer
- 티스토리챌린지
- 오블완
- Object Oriented Programming
- raw data
- Data Science
- 포인터
- vscode
- function
- string
Archives
- Today
- Total
Channi Studies
[C++] While Loop 본문
Syntax
// syntax
while (expression)
statement;
// or
while (expression) {
statement(s);
}
중요한 점은 infinite loop이 되지 않도록 신경쓰는 것입니다.
Example Codes
// Example 1
#include <iostream>
using namespace std;
int main(){
int i {1};
while (i <= 5) {
cout << i << endl;
i++;
}
}
/*
Output:
1
2
3
4
5
*/
i++를 통해 loop 1번당 i를 1씩 증가시키지 않는다면,
i를 초기화때의 값 1에 머물러 있을 것이고,
while문의 조건에서 영원히 벗어날 수가 없으니
1을 무한으로 출력하는 프로그램이 될 것입니다.
// Example 2
// while loop 내부에서 if문 작성
#include <iostream>
using namespace std;
int main(){
int i {1};
while (i <= 10) {
if (i % 2 == 0)
cout << i << endl;
i++;
}
}
/*
Output:
2
4
6
8
10
*/
// Example 3
// while loop for vector
#include <iostream>
using namespace std;
int main(){
vector<int> my_nums {10, 100, 1000};
int i = 0;
while (i < my_nums.size()){
cout << my_nums[i] << endl;
i++;
}
}
/*
Output
10
100
1000
*/
// Example 4
// using boolean for while loop
#include <iostream>
using namespace std;
int main(){
bool done {false};
int num {0};
cout << "Enter a number between 1 and 5: ";
cin >> num;
while (num <= 1 || num >= 5) {
cout << "Out of range.\nEnter a number between 1 and 5 again: ";
cin >> num;
}
cout << "You entered " << num << ", which is in between 1 and 5." << endl;
cout << "Thank you!" << endl;
}
/*
Output
Enter a number between 1 and 5 again: 5
Out of range.
Enter a number between 1 and 5 again: 100
Out of range.
Enter a number between 1 and 5 again: 3
You entered 3, which is in between 1 and 5.
Thank you!
*/
// Example 5
// using boolean and if in while loop
#include <iostream>
using namespace std;
int main(){
int num {};
cout << "Enter a number in between 1 and 5: ";
cin >> num;
bool done = false;
while (!done){
if (num <= 1 || num >= 5) {
cout << "\nYou entered " << num << endl;
cout << "Out of range. Please re-enter a number between 1-5: ";
cin >> num;
}else{
cout << "\nYou entered " << num << endl;
cout << "Thank you for entering a valid number." << endl;
done = true;
}
}
}
/*
Output:
Enter a number in between 1 and 5: 10
You entered 10
Out of range. Please re-enter a number between 1-5: 1
You entered 1
Out of range. Please re-enter a number between 1-5: 5
You entered 5
Out of range. Please re-enter a number between 1-5: -5
You entered -5
Out of range. Please re-enter a number between 1-5: 3
You entered 3
Thank you for entering a valid number.
*/
'C++ > 반복문 (Loop)' 카테고리의 다른 글
[C++] do-while Loop (0) | 2023.12.12 |
---|---|
[C++] Range-based for Loop (0) | 2023.12.10 |
[C++] Conditional Operator (조건 연산자) (2) | 2023.12.08 |