C++

[C++ 3-4] if / if else 구문

KimKyao 2024. 10. 7. 21:47
#include <iostream>


using namespace std;


int main()
{
    // 
    //if (조건)
    //    코드

    if (true)
        cout << "조건이 참" << endl;
    if (true) {
        cout << "조건이 참" << endl;
        cout << "조건이 참" << endl;
    }
    if (false)
        cout << "조건이 거짓, 출력 안됨" << endl;

    if (false) {
        cout << "조건이 참, 출력 안됨" << endl;
    }
    else {
        cout << "조건이 거짓, 출력 됨" << endl;
    }

    if (false) {
        cout << "#1";
    }
    else {
        if (true) {
            cout << "#2";
        }
        else {
            cout << "#1";
        }
    } 
    // 아래 처럼 else 다음 if 사이의 중괄호를 생략할 수 있다.

    if (false) {
        cout << "#1";
    }
    else if (true) {
        cout << "#2";
    }
    else {
        cout << "#1";
    }


    return 0;
}