따배씨++ (3.6 Logical Operator)

김동우·2021년 4월 13일
0

안녕하십니까. 김동우입니다.

이번 노트에서는 다양한 논리연산자에 대한 내용을 얘기할까 합니다.

그럼 바로 코드부터 보겠습니다.

  1. main.cpp
#include <iostream>

int main()
{   
    using namespace std;

    cout << boolalpha;
    // logical NOT
    bool x = true;

    cout << !x << endl; // output : false
    // logical AND
    bool a = true;
    bool b = false;

    cout << '\n' << (a && b) << endl; // ourput : false

    bool hit = true;
    int health = 10;

    if(hit == true && health < 20)
    {
        cout << '\n' << "die" << endl;
    }
    else
        health -= 20;
    // output : die

    // logical OR
    bool i = true;
    bool j = false;

    cout << '\n' << (i || b) << endl;

    // ex
    int n = 5;
    int m = 7;

    if (!n == m)
    {
        cout << '\n' << " x does not equal y " << endl;
    }
    else
        cout << '\n' << " x equals y " << endl;
    // output : x equals y

    // bool type 형변환에 의해 좌항 !n -> false, m -> true가 되기 때문에
    // 같지 않으므로, else로 넘어가서 출력값을 가지게 된다.
    // vscode에서는 친절히 경고메세지를 출력해준다.

    if (!(n == m))
    {
        cout << '\n' << " x does not equal y " << endl;
    }
    else
        cout << '\n' << " x equals y " << endl;
    // output : x does not euqal y

    // associativity를 잘 생각해보자.

    int v = 1;

    if (v == 0 || v == 1)
    {
        cout << '\n' << " v is 0 or 1 " << endl;
    }
    // output : v is 0 or 1

    // short circuit evaluation
    int x2 = 1;
    int y2 = 2;
    int z2 = 2;

    if (x2 == 1 && y2++ == 2)
    {
        //do something
    }

    cout << '\n' << y2 << endl; // output : 3

    if (x2 == 100 && z2++ == 2)
    {
        //do something
    }
    cout << '\n' << z2 << endl; // output : 2

    // AND operator의 연산에서 좌항이 false일 경우 전체 false return을
    // 신속하게 하기 위해 우항의 연산자를 무시하게 된다.
    // 이에 y++는 좌항이 true라 계산되지만, z++는 무시하는 것이다.

    // De Morgan's Law
    bool x3 = true;
    bool y3 = false;

    // !(x3 && y3);
    // !x3 && !y3;
    // 위처럼 분배법칙은 적용되지 않는다.

    // !(x3 && y3); == !x3 || !y3
    // !x3 && !y3; == !(x3 || y3)
    // 다만, 위와 같이는 표현할 수 있다.
    // 논리게이트의 부정에 대한 글을 참고해보자
    // https://ko.wikipedia.org/wiki/%EB%93%9C_%EB%AA%A8%EB%A5%B4%EA%B0%84%EC%9D%98_%EB%B2%95%EC%B9%99

    // XOR gate (x3 != y3)
    // false    false   false
    // false    true    true
    // true     false   true
    // true     true    false

    bool v1 = true;
    bool v2 = false;
    bool v3 = false;

    bool r1 = v1 || v2 && v3;
    bool r2 = (v1 || v2) && v3;
    bool r3 = v1 || (v2 && v3);
    // associativity test

    cout << r1 << " " << r2 << " " << r3 << endl;
    // output : true false
    // 즉, 위 두 연산은 다른 연산이 된다.
    // r1 == r3인 이유는, AND가 OR에 비해 우선순위가 높기 때문이다.

    return 0;
}
  1. test.cpp
#include <iostream>

int main()
{   
    using namespace std;

    
    cout << (( true && true ) || false) << endl;
    // expect : true, output : true

    cout << (( false && true ) || true) << endl;
    // expect : true, output : true

    cout << (( false && true ) || false || true) << endl;
    // expect : true, output : true

    cout << (( 14 > 13 || 2 > 1 ) && ( 9 > 1 )) << endl;
    // expect : true, output : true

    cout << !( 2314123 > 2 || 123123 > 2387 ) << endl;
    // expect : false, output : false

    return 0;

}

그럼 이번 글은 여기서 마치도록 하겠습니다. 감사합니다.

0개의 댓글

관련 채용 정보