OR operator and AND operator. Confuse???

Can anyone help me. I always confuse when typing && operator and || operator. I confuse when there have 2 or more operator.

 
  statement  ||    statement|| statement


or

 
statement  &&    statement  && statement
Why does it confuse you?
Last edited on
closed account (SECMoG1T)
1
2
 
statement  ||    statement|| statement


this condition will be true if one or more of the statement is true , you can draw a small truth table just to help you see how


true || true|| false == true
true||false|| false == true
true || false || true ==true.
false||false||false== false

the condition can only be false only if all conditions are false.

2.
statement && statement && statement

This one seem much as the opposite of the one above, that condition can only be true only if all the statements are true... a truth table will be like this


false&& true&& true == false;
false && false && true== false;
true&& true&& true  == true;
false&&false&&false == false;


hope that helps
Last edited on
Thanks andy1992.
If you're also confused about precedence, the right-most is evaluated first:

if (a && b || c)

means

if (a && (b || c))

and

if (b || c && a)

means

if (b || (c && a))


Anyway, I always use parentheses in those cases, makes it a whole lot less confusing.

EDIT: I was wrong.
EDIT2: OK, this whole thing is getting me very confused.
Last edited on
fg109 wrote:
the right-most is evaluated first:

Incorrect. If the operators are of the same precedence, the evaluation is left to right.
http://www.cplusplus.com/doc/tutorial/operators/
When using the logical operators, C++ only evaluates what is necessary from left to right to come up with the combined relational result, ignoring the rest. Therefore, in the last example ((5==5)||(3>6)), C++ evaluates first whether 5==5 is true,
Sorry, my code is ugly. But I don't see anything wrong with it. The results have left me very confused.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>

using namespace std;

void printTest(bool a, bool b, bool c)
{
    cout << "a = " << a << ", b = " << b << ", c = " << c << endl;
    cout << "(a && b || c) = ";
    if (a && b || c)
        cout << "true" << endl;
    else
        cout << "false" << endl;
    cout << "((a && b) || c) = ";
    if ((a && b) || c)
        cout << "true" << endl;
    else
        cout << "false" << endl;
    cout << "(a && (b || c)) = ";
    if (a && (b || c))
        cout << "true" << endl;
    else
        cout << "false" << endl;
    cout << "(a || b && c) = ";
    if (a || b && c)
        cout << "true" << endl;
    else
        cout << "false" << endl;
    cout << "((a || b) && c) = ";
    if ((a || b) && c)
        cout << "true" << endl;
    else
        cout << "false" << endl;
    cout << "(a || (b && c)) = ";
    if (a || (b && c))
        cout << "true" << endl;
    else
        cout << "false" << endl;
}

int main()
{
    bool a, b, c;
    a = b = c = true;
    for (int i = 0; i < 2; ++i)
    {
        for (int j = 0; j < 2; ++j)
        {
            for (int k = 0; k < 2; ++k)
            {
                printTest(a, b, c);
                cout << endl << endl;
                c = !c;
            }
            b = !b;
        }
        a = !a;
    }
    return 0;
}

The output:
a = 1, b = 1, c = 1
(a && b || c) = true
((a && b) || c) = true
(a && (b || c)) = true
(a || b && c) = true
((a || b) && c) = true
(a || (b && c)) = true


a = 1, b = 1, c = 0
(a && b || c) = true
((a && b) || c) = true
(a && (b || c)) = true
(a || b && c) = true
((a || b) && c) = false
(a || (b && c)) = true


a = 1, b = 0, c = 1
(a && b || c) = true
((a && b) || c) = true
(a && (b || c)) = true
(a || b && c) = true
((a || b) && c) = true
(a || (b && c)) = true


a = 1, b = 0, c = 0
(a && b || c) = false
((a && b) || c) = false
(a && (b || c)) = false
(a || b && c) = true
((a || b) && c) = false
(a || (b && c)) = true


a = 0, b = 1, c = 1
(a && b || c) = true
((a && b) || c) = true
(a && (b || c)) = false
(a || b && c) = true
((a || b) && c) = true
(a || (b && c)) = true


a = 0, b = 1, c = 0
(a && b || c) = false
((a && b) || c) = false
(a && (b || c)) = false
(a || b && c) = false
((a || b) && c) = false
(a || (b && c)) = false


a = 0, b = 0, c = 1
(a && b || c) = true
((a && b) || c) = true
(a && (b || c)) = false
(a || b && c) = false
((a || b) && c) = false
(a || (b && c)) = false


a = 0, b = 0, c = 0
(a && b || c) = false
((a && b) || c) = false
(a && (b || c)) = false
(a || b && c) = false
((a || b) && c) = false
(a || (b && c)) = false


These seem to imply that it's evaluated left to right.
a = 0, b = 1, c = 1
(a && b || c) = true
((a && b) || c) = true
(a && (b || c)) = false

[...]
a = 0, b = 0, c = 1
(a && b || c) = true
((a && b) || c) = true
(a && (b || c)) = false



These seem to imply that it's evaluated right to left.
a = 1, b = 1, c = 0
[...]
(a || b && c) = true
((a || b) && c) = false
(a || (b && c)) = true

[...]

a = 1, b = 0, c = 0
[...]
(a || b && c) = true
((a || b) && c) = false
(a || (b && c)) = true
Parenthesis change the order of evaluation. I'm not sure why you would expect any different?

It's not possible to determine the order of evaluation unless you see the consequences of it, as with the example I posted where it does not print things that are not evaluated.
Last edited on
&& has higher precedence than || so (a && b || c) is the same as ((a && b) || c), and (a || b && c) is the same as (a || (b && c)).
Peter87 wrote:
&& has higher precedence than || so (a && b || c) is the same as ((a && b) || c), and (a || b && c) is the same as (a || (b && c)).


Thanks, for some reason I was under the impression that they had the same precedence.
There is a difference between order of evaluation (getting the values to use in the expression) and order of operations (operator precedence). I think these two different concepts are being confused.
Topic archived. No new replies allowed.