======C++ 스터디 #2: 연산자, 조건, 반복====== | 시간 | 2021년 3월 25일 목요일 20:30 ~ 22:10 | | 장소 | Google Meet | | 참가자 | - | {{youtube>N2Jf-6541SU?medium}} \\ 출처: https://dojang.io/course/view.php?id=2 (코딩도장) =====1. 관계 연산자===== 관계 연산자는 값을 비교할 때 사용하며 연산자의 종류는 다음과 같습니다. {{activity:public:2021:cpp:관계.png?200}} ====1.1. 관계 연산자 예시==== #include using namespace std; int main(void) { int x = 1, y = 2; cout << "== 관계 연산자: " << (x == y) << endl; cout << "!= 관계 연산자: " << (x != y) << endl; cout << "> 관계 연산자: " << (x > y) << endl; cout << "< 관계 연산자: " << (x < y) << endl; cout << ">= 관계 연산자: " << (x >= y) << endl; cout << "<= 관계 연산자: " << (x <= y) << endl; return 0; } =====2. 논리 연산자===== 논리 연산자는 조건식이나 값을 논리적으로 판단합니다. 논릿값 거짓(false)은 0, 참(true)은 0이 아닌 값이며 보통 1을 사용합니다. {{activity:public:2021:cpp:논리.png?400}} ====2.1. 논리 연산자 예시==== #include using namespace std; int main(void) { int x = 5, y = 6, z=7; cout << "&& 논리 연산자 True: " << ((x < y) && (y < z)) << endl; cout << "|| 논리 연산자 True: " << ((x < y) || (y > z)) << endl; cout << "! 논리 연산자 True: " << (!(x > y)) << endl; cout << "&& 논리 연산자 False: " << ((x < y) && (y > z)) << endl; cout << "|| 논리 연산자 False: " << ((x > y) || (y > z)) << endl; cout << "! 논리 연산자 False: " << (!(x < y)) << endl; return 0; } =====3. if문===== if 조건문은 괄호 안에 조건식을 지정하여 사용합니다. {{activity:public:2021:cpp:if.png?100}} ====3.1.1 if 반복문 예시(1)==== #include using namespace std; int main() { int x = 10; if (x == 10) { cout << "10입니다." << endl; } return 0; } ====3.1.2 if 반복문 예시(2): else ==== #include using namespace std; int main() { int x = 20; if (x == 10) { cout << "10입니다." << endl; } else { cout << "10이 아닙니다." << endl; } return 0; } ====3.1.3 if 반복문 예시(3): else if==== #include using namespace std; int main() { int x = 20; if (x == 10) { cout << "10입니다." << endl; } else if(x>10) { cout << "10보다 큽니다." << endl; } else { cout << "10보다 작습니다." << endl; } return 0; } ====3.1.4 if 반복문 예시(4): 논리 연산자==== #include using namespace std; int main() { int x = 10, y = 20; if (xy) { cout << "x는 y보다 크다." << endl; } else { cout << "x와 y는 같다." << endl; } return 0; } ====3.1.5 if 반복문 예시(5): 관계 연산자==== #include using namespace std; int main() { int x = 10, y = 20, z=30; if ((x < y) && (x < z)) { cout << "x는 y와 z보다 작다." << endl; } if (!(x > y)) { cout << "x가 y보다 크다" << endl; } return 0; } =====4. for 반복문===== for 반복문은 반복되는 작업을 간단하게 처리해줍니다. {{activity:public:2021:cpp:for1.png?500}} {{activity:public:2021:cpp:for2.png?500}} ====4.1.1 for 반복문 예시(1)==== #include using namespace std; int main() { for (int i = 0; i < 100; i++) { cout<<"Hello, world! "< ====4.1.2 for 반복문 예시(2): 이중 for문==== #include using namespace std; int main() { for (int i = 5; i > 0; i--) { for (int j = 5; j > 0; j--) { cout << "(" << i << "," << j << ") "; } cout << endl; } return 0; } =====5. while 반복문===== while 반복문은 괄호 안에 조건식만 들어가고, 초기식은 반복문 바깥에 있습니다. 그리고 중괄호 안에 반복할 코드와 변화식이 함께 들어갑니다. {{activity:public:2021:cpp:while.png?500}} ====5.1. while 반복문 예시(1)==== #include using namespace std; int main() { int i = 0; while (i < 100) { cout<< "Hello, world! "< =====6. break===== break는 for, while 문법에서 반복을 벗어나기 위해 사용합니다. ====6.1. break 예시(1)==== #include using namespace std; int main() { int i = 0; while (1) { i++; cout << i << endl; if (i == 100) break; } return 0; } =====7. continue===== continue는 반복을 유지한 상태에서 코드의 실행만 건너뛰는 역할을 합니다. 마치 카드 게임을 할 때 패가 안 좋으면 판을 포기하는 것과 같습니다. ====7.1. continue 예시(1)==== #include using namespace std; int main() { for (int i = 1; i <= 100; i++) { if (i % 2 != 0) continue; cout << i << endl; } return 0; } =====8. 예제===== ====8.1. 예제(1): 입력된 숫자가 짝수인지 홀수인지 판별해주는 프로그램==== #include using namespace std; int main() { int number; cout << "Enter an integer: "; cin >> number; if (number % 2 == 0) { cout << number << " is even" << endl; } else cout << number << " is odd" << endl; return 0; } ====8.2. 예제(2): 점수를 입력하면 등급을 알려주는 프로그램==== #include using namespace std; int main() { int score; cout << "Enter your score: "; cin >> score; if (score>= 90) cout << "Grade: A" << endl; else if (score >= 80) cout << "Grade: B" << endl; else if (score >= 70) cout << "Grade: C" << endl; else if (score >= 60) cout << "Grade: D" << endl; else cout << "Grade: F" << endl; return 0; } ====8.3. 예제(3): 점의 좌표를 입력하면 점이 몇 사분면에 있는지 판별해주는 프로그램==== #include using namespace std; int main() { double x, y; int quadrant; cout << "Enter the x-coordinate value: "; cin >> x; cout << "Enter the y-coordinate value: "; cin >> y; if ((x > 0) && (y > 0)) quadrant = 1; else if ((x < 0) && (y > 0)) quadrant = 2; else if ((x < 0) && (y < 0)) quadrant = 3; else quadrant = 4; cout << "Quadrant: " << quadrant; return 0; } ====8.4. 예제(4): 5부터 500까지 차례대로 5의 배수만 출력하는 프로그램==== ===8.4.1 for문=== #include using namespace std; int main() { for (int i = 1; i <= 100; i++) { cout << 5 * i << endl; } return 0; } ===8.4.2 continue 사용하기=== #include using namespace std; int main() { for (int i = 1; i <= 500; i++) { if (i % 5 != 0) continue; cout << i << endl; } return 0; }