How to make equal comparison in C or C ++?

I'm just curious in C or C ++, for expression:

b == c || b == d

Can I do something like:

b == (c || d)

and get the same behavior?

+4
source share
2 answers

The first expression b == c || b == dwill give you a value of true if beither cor d.

The second expression b == (c || d)will check only if it is beither 0 or 1, because the output c || dis binary.

Consider this code:

#include <iostream>
using namespace std;

int main() {
    int b=10,c=9,d=10;
    cout << (b ==c || b ==d )<<endl;
    cout<< ( b == ( c || d)) <<endl;
    d=11;
    cout << (b ==c || b ==d )<<endl;
    cout<< ( b == ( c || d)) <<endl;
    return 0;
}

Output signal

1
0
0
0

Now you can clearly see that both expressions are not the same.

+5
source

, C ++ . . "", .

, , , , , , , .

|| 1 true, true ( ), 0 false, ( ), ; . ( C int, ++ - bool.)

b == c || b == d

(b == c) || (b == d)

, b c, b d. :

b == (c || d)

(c || d), , b .

,

x < y < z

(x < y) && (y < z)

,

(x < y) < z

false true ( ++) 0 1 ( C) x < y z.

+2

Source: https://habr.com/ru/post/1527146/


All Articles