C++ 스터디 #2: 연산자, 조건, 반복

시간 2021년 3월 25일 목요일 20:30 ~ 22:10
장소 Google Meet
참가자 -


출처: https://dojang.io/course/view.php?id=2 (코딩도장)

관계 연산자는 값을 비교할 때 사용하며 연산자의 종류는 다음과 같습니다.

#include <iostream>
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;
}

논리 연산자는 조건식이나 값을 논리적으로 판단합니다. 논릿값 거짓(false)은 0, 참(true)은 0이 아닌 값이며 보통 1을 사용합니다.

#include <iostream>
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;
}

if 조건문은 괄호 안에 조건식을 지정하여 사용합니다.

#include <iostream>
using namespace std;

int main()
{
    int x = 10;

    if (x == 10)
    {
        cout << "10입니다." << endl;
    }

    return 0;
}

#include <iostream>
using namespace std;

int main()
{
    int x = 20;

    if (x == 10)
    {
        cout << "10입니다." << endl;
    }
    else
    {
        cout << "10이 아닙니다." << endl;
    }

    return 0;
}

#include <iostream>
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;
}

#include <iostream>
using namespace std;

int main()
{
    int x = 10, y = 20;

    if (x<y)
    {
        cout << "x는 y보다 작다." << endl;
    }
    else if(x>y)
    {
        cout << "x는 y보다 크다." << endl;
    }
    else
    {
        cout << "x와 y는 같다." << endl;
    }

    return 0;
}

#include <iostream>
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;
}

for 반복문은 반복되는 작업을 간단하게 처리해줍니다.

#include <iostream>
using namespace std;

int main()
{
    for (int i = 0; i < 100; i++)
    {
        cout<<"Hello, world! "<<i<<endl;
    }

    return 0;
}

#include <iostream>
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;
}

while 반복문은 괄호 안에 조건식만 들어가고, 초기식은 반복문 바깥에 있습니다. 그리고 중괄호 안에 반복할 코드와 변화식이 함께 들어갑니다.

#include <iostream>
using namespace std;

int main()
{
    int i = 0;
    while (i < 100)
    {
        cout<< "Hello, world! "<<i<<endl;
        i++;
    }

    return 0;
}

break는 for, while 문법에서 반복을 벗어나기 위해 사용합니다.

#include <iostream>
using namespace std;

int main()
{
    int i = 0;

    while (1)
    {
        i++;

        cout << i << endl;

        if (i == 100)
            break;
    }

    return 0;
}

continue는 반복을 유지한 상태에서 코드의 실행만 건너뛰는 역할을 합니다. 마치 카드 게임을 할 때 패가 안 좋으면 판을 포기하는 것과 같습니다.

#include <iostream>
using namespace std;

int main()
{
    for (int i = 1; i <= 100; i++)
    {
        if (i % 2 != 0)
            continue;

        cout << i << endl;
    }

    return 0;
}

#include <iostream>
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;
}

#include <iostream>
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;
}

#include <iostream>
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.1 for문

#include <iostream>
using namespace std;

int main()
{
    for (int i = 1; i <= 100; i++)
    {
        cout << 5 * i << endl;
    }

    return 0;
}

8.4.2 continue 사용하기

#include <iostream>
using namespace std;

int main()
{
    for (int i = 1; i <= 500; i++)
    {
        if (i % 5 != 0)
            continue;

        cout << i << endl;
    }

    return 0;
}

  • activity/public/2021/cpp/210325.txt
  • 마지막으로 수정됨: 3년 전
  • 저자 david