본문 바로가기
개인 개발 공부/C ++

조건문

by minjoothi 2024. 11. 9.

조건이 1개 일 때 

#include <iostream>

int main()
{
    using namespace std;

    int carrots = 1;

    if (carrots < 5)
    {
        cout << "Let's buy new carrots!" << endl;
        //buy new carrots
    }
    else
    {
        cout << "No need to buy some carrots!" << endl;
        // no need to buy carrots.
    }

    return 0;
}

조건이 여러개 일 때

#include <iostream>

int main()
{
    using namespace std;

    int carrots = 7;

    if (carrots < 5)
    {
        cout << "Let's buy new carrots!" << endl;
        //buy new carrots
    }
    else if (carrots < 10)
    {
        cout << "Ummmm....." << endl;
    }
    else
    {
        cout << "No need to buy some carrots!" << endl;
        // no need to buy carrots.
    }

    return 0;
}

과제

 

#include <iostream>

int main() {
    using namespace std;

    int year;
    int month;
    int day;
    int birth;

    cout << "몇년도에 태어나셨나요? " << endl;
    cin >> year;

    cout << "몇월에 태어나셨나요? " << endl;
    cin >> month;

    cout << "며칠에 태어나셨나요? " << endl;
    cin >> day;

    cout << year << "년" << month << "월" << day << "생이군요!" << endl;

    if (year < 2004)
    {
        cout << " 출입가능 " << endl;
    }
    else
    {
        cout << " 출입금지 " << endl; 
    }

 

'개인 개발 공부 > C ++' 카테고리의 다른 글

1-6 함수  (1) 2024.11.10
1-5 반복문과 다중반복문  (0) 2024.11.09
연산자  (0) 2024.11.09
1-2. 변수와 자료형 그리고 입출력 함수  (0) 2024.11.09
GPT를 통해 알아본 유니티와 언리얼, C#과 C++  (0) 2024.11.08