3
if(1 == 2 || 4)
{
cout<<"True";
}
else
{
cout<<"False";
}

This is how I read the above. If 1 is equal to 2 or 4, then print true. Otherwise, print false. When this is executed though... true is printed. Obviously I'm misunderstanding something here. 1 is not equal to 2 or 4. Wouldn't that make it false?

halfer
  • 19,824
  • 17
  • 99
  • 186
Mike York
  • 41
  • 1
  • 1
  • 2
  • Adding parenthesis to clarify the expression may help you see what is happening: `if ( (1 == 2) || 4)` – Neil Kirk Oct 29 '15 at 19:27
  • Thank you every one for the comments. I understand now (I think). My confusion comes from a false premise, I believe. In logic, there are logical disjunctions the statement "A or B" is true if A is true. The statement "A or B" is true if B is true. The statement "A or B" is false when neither A or B is true. I looked at (if(1 == 2 || 4) as initially neither true or false but becomes false once it is evaluated, since 1 is not equal to 2 or 4. I guess I've just been looking at it in the wrong way. Thanks again, every one. – Mike York Oct 29 '15 at 19:38
  • I'm a beginner to C++ (and programming in general) and I've just begun reading programming: principles and practice using C++ by bjarne stroustrup. I had to write an if statement using a logical or operator but something wasn't coming up right. Now I understand that I was looking at it in the wrong way. – Mike York Oct 29 '15 at 19:52
  • 1
    Possible duplicate of [Can you use 2 or more OR conditions in an if statement?](https://stackoverflow.com/q/8781447/608639) – jww Sep 24 '18 at 01:16

5 Answers5

9

Yeah, I've made the same mistake.

Read the sentence again:

If 1 is equal to 2 or 4, then print true.

The "2" and "4" both refer to the "If 1 is equal to [...]." That means, the sentence is just an abbreviation of

If 1 is equal to 2 or 1 is equal to 4, then print true.

This leads us to the if-clause

if (1 == 2 || 1 == 4)

instead.


1 == 2 || 4 is true because (1 == 2) == false ORed with 4 == true, yields true (false OR true = true).

cadaniluk
  • 15,027
  • 2
  • 39
  • 67
2

This is how I read the above. If 1 is equal to 2 or 4, then print true. Otherwise, print false. When this is executed though... true is printed. Obviously I'm misunderstanding something here. 1 is not equal to 2 or 4. Wouldn't that make it false?

No, 1 is equal to 2 or 4. 4 is true (because it's not zero). So anything "or 4" is also true. Therefore "1 is equal to 2 or 4" is true.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
1

In this your phrase

If 1 is equal to 2 or 4, then print true

you need to add a pair of words that to get a correct equivalent C++ phrase

If 1 is equal to 2 or is equal to 4, then print true

Thus it will look the following way

if ( 1 == 2 or 1 == 4 )
{
    cout << "True";
}
else
{
    cout<<"False";
}

As for the original condition

1 == 2 || 4

then the compiler consideres it the following way (due to the priorities of the operators):

( 1 == 2 ) || 4

According to the C++ Stanbdard

The operators == and != both yield true or false, i.e., a result of type bool.

So as 1 == 2 is equal to false then you get

false || 4

where 4 as it is not equal to 0 is converted to boolean true and as result the entire condition evaluates to true

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

This not how if statements work in C++. If you want to know if something is equal to one thing or another thing then it is

if (this == this_thing || this == another_thing)

What you have

if(1 == 2 || 4)

Gets evaluated to

if ((1 == 2) || 4)
if (false || true)
if (true)

So the if statement will always be true.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
0

It's evaluating to true because if you check for the boolean value of just an integer (like you're doing with the number 4), it will evaluate to true. Like the people said above, you have to split it up.

Derek Schuster
  • 523
  • 1
  • 6
  • 14