布林運算

非零即為真。

在前面 變數-Bool 布林值 時有提過:0代表False;1代表True。

而在這裡還要再補充一點:只要不是0,就是真。例如:

bool b1 = 2;
bool b2 = -1;
cout << b1 << endl << b2 << endl;

範例輸出:

1

1

關係運算子

來看Code吧

#include <iostream>
using namespace std;

int main() {
    int a = 10;
    int b = 5;
    int c = 10;
    
    cout << (a > b) << endl;
    cout << (c < b) << endl;
    cout << (a != c) << endl;
    
    return 0;
}

範例輸出:

1

0

0

邏輯運算子

範例程式

#include <iostream>
using namespace std;

int main() {
    int a = 5;
    int b = 10;
    int c = 5;
    
    cout << ((a == b) || (a == c)) << endl;
    cout << !((a == b) || (a == c)) << endl;
    cout << ((a < b) && (a == c)) << endl;
    
    return 0;
}

範例輸出:

1

0

1

Last updated