Variable x equals a or b - c++

Is this:
if(x == a || b){//do something}
same as:
if(x == a || x == b){//do something}
?
I think it is not, because in the first case we evaluate if x equals a and if b is true or false.
In the second case, we evaluate if x equals a and if x equals b. And I understand that in the lazy evaluation if x equals a than we are not evaluating further.
But somebody thinks that in the first case we ask if x equals a or b, so I wanna make sure.

No.
In C++, this:
x == a || b // Same as (x == a) || b
Is equivalent to this:
(x == a) || (bool)b
Which evaluates to true if x and a are equal OR if b evaluates to true when converted to bool. In C, on the other hand, it is equivalent to this:
(x == a) || (b != 0)
Which evaluate to true if x and a are equal OR if b is different from 0 (here we must make the implicit assumption that b is of integral type, otherwise this won't compile).
On the other hand, this:
(x == a || x == b) // Same as ((x == a) || (x == b))
Evaluates to true when either x and a are equal OR x and b are equal (i.e., if x is either equal to a or equal to b) both in C++ and in C.

The two expressions are not equivalent. This
if(x == a || b)
is the equivalent of
if( (x == a) || (b))
i.e an OR of x==a and b. In C++, if b evaluates to anything other than 0 or false, it is taken as true.
The second one tests whether x==b instead of simply testing b.

No. In C this is equivalent to:
if(x == a || b != 0)

The first reads as "if x is equal to a, or if b is truthy"
The second reads as "if x is equal to a, or if x is equal to b"

No.
if (x == a || b)
is equal to
if ((x == a) || (bool)b)
because operator == has higher precedence than operator ||. See Operator Precedence Table.

You are almost right, the first case means x equals a OR b is true.
Lazy evaluation means that the expression will be evaluated only until the result is obvious. In an OR expression, for example (x || y), the result will be known when x==true – then the whole expression must be true too. In the case of AND, like (x && y), the result will be evident when x==false. So you are right, if x==a, we know the answer already and no more work is needed.

Related

How Precedence of Relational Operator handled in C++

According to precedence rules <, >, <=, >= has precedence over !=, ==
I am so confused that how the following statement will be executed
int a=3, b=3;
cout<< (a != b || a <= b);
I know short circuit evaluation and according to precedence rules I guess that compiler will execute a <= b first as it has precedence over != but it is not doing so.
I did little experiment with above statement by changing a <= b-- and changing order of above conditions and it seems that <= and != have same precedence as compiler execute whichever occurred first. Or I am missing something?
Precedence of operators is only relevant to how expressions are bound, not to how they're executed. Execution order is dependent on the "happens-before" relationship and otherwise subject to arbitrary reordering by the compiler.
Relative precedence of two operators also only matters if they are directly adjacent. In a == b <= c, you get a == (b <= c), not (a == b) <= c. In a == b || c <= d, the adjacent pairs are == and || and || and <=. In both cases the comparison operators bind more strongly, so you get (a == b) || (c <= d), making the relative precedence of == and <= irrelevant. If you have a == b + c <= d instead, you first get a == (b + c) <= d, and now you need to compare == and <= again, getting you a == ((b + c) <= d).
As for order of evaluation, || has a rule that its left side is sequenced before its right side (assuming it's not overloaded), because the right side might not get evaluated at all. So the == is executed first. But precedence plays no part at all in this. If you instead had written the non-short-circuiting a != b | a <= b, both sides would eventually get executed (with caveats, see below), but there are no guarantees which side gets evaluated first; precedence does not play a part here.
Caveat: the compiler can still just optimize your code and realize that a != b || a <= b is tautological, and simply replace the entire thing with true.
Precedence is not order of evaluation.
Precedence is about where to put parentheses. a != b || a <= b is parsed as: (a != b) || (a <= b). It's not parsed as like for example: a != (b || (a <= b)) or any other combination of ( ).
After we know where parentheses are, then we can evaluate the expression. The order of evaluation is: (a != b) first. Then || is evaluated. Then, optionally, a <= b is evaluated.
From: https://en.cppreference.com/w/cpp/language/operator_logical
You are looking for the part in ( )
Builtin operators && and || perform short-circuit evaluation (do not evaluate the second operand if the result is known after evaluating the first), but overloaded operators behave like regular function calls and always evaluate both operands
So a != b will be evaluated first.
In your case 3 != 3 so the second operand will be evaluated.
if( (5<5) || (5!=5) || (5==5) || (5>5) )
The precedence left to right, so 5<5, goes to next 5!=5 goes to next 5==5 operand if returns true and 5>5 is not evaluated.
As per Microsoft doc,
The operator <= >= < > has higher precedence than the operator != ==. But as you have applied the logical or || operator the expression will be executed from left to right.
It will handle it as ( ( a != b ) || ( a <= b ) ) meaning, first ( a != b ) will be check, after that, logical ||, and if there's need than this expression ( a <= b ) will be checked ( only if the first expression ( a != b ) is wrong).

Evaluation of logical condition in ternary operator

I do not know whether parens are required before the ternary operator.
Example:
int a,b,d,e,f; // Some numbers
int l = ( a > b || d < e ) ? a : d;
is surely fine, if a>b or d<e then l = a else l = d.
I am not sure if instead
int l = a > b || d < e ? a : d;
is equally valid, and what about longer statements?
int l = ( a > b || d ) && e > f ? a : d;
I guess that my question is: the ternary operator always treat whatever comes before him as a logical expression and evaluates it, and therefore there is no need for parens before it?
Your first example is correct.
And the second statement is equally valid to the first one.
Your second example reads as follows:
(...) have highest precedence and > and < have higher precedence than && which has higher precedence than || which has higher precedence than ?:.
So
if the result of a > b || d (d is converted to 1 if different than 0) is true
and
if the result of e > f is true
then
l=a
otherwise l=b.

Odd if statements

I didn't think these if's would compile but they do:
if (a>>b&&c&&d)
if (month==1,2,3,5,7,9,10)
The first I'm clueless about. In the second statement is the comma supposed to be an (||) or operator ?
Syntax wise was it always this way or was it introduced some time ago ?
I'm using Visual Studio 2010.
if (a>>b && c && d)
it is equal to
if ((a>>b) && c && d)
if the result of a shifted right b times evaluates to a bool, c and d also evaluates to bool respectively, then all these booleans will be AND-ed to each other.
In your context, the all expressions within commas will be evaluated and then the last expression will be passed to if expression:
if (month==1,2,3,5,7,9,10) -> is equal to
if (2,3,5,7,9,10) -> is equal to
if (3,5,7,9,10) -> is equal to
if (5,7,9,10) -> is equal to
if (7,9,10) -> is equal to
if (9,10) -> is equal to
if (10)
which is always true.
It's not suppose to be || or &&. If you want OR or AND write it like below:
if (month==1 || month==2 || month==3 || ....)
or
if (month==1 && month==2 && month==3 && ....)
// Also month can not simultaneously be equal to more than one value!
// then, it's equal to
if (false)
The first if statement would be evaluated like:
if(((a >> b) && c) && d)
Essentially bitshift a by b bits and then logical and with c and then with d
The second is the comma operator which will evaluate the first term and throw it away, then the second, and so on and return the result of the final term. So in our case the statement is equivalent to:
if(10)
which is always true.

Comparison of int values in an if statement

I had a question in my test paper in which we had to compare the values of int type variables. The first thought that came to my mind was that it was missing the && operator but i am not sure.
int a=2, b=2, c=2;
if(a==b==c)
{
printf("hello");
}
I have a doubt, will the above statement will execute or not in c or c++? Can i have the reason as well.
Thank You
It will execute but with what I believe unexpected results to you.
One of the == will evaluate to a boolean value, which will then be converted to an int and then the second comparison will be performed, comparing an int to either 1 or 0.
The correct statement is a==b && b==c.
For example:
3 == 3 == 3
evaluates to
true == 3
1 == 3
false
a==b==c
is equivalent to
(a == b) == c
The result of a == b is 1 (if true) or 0 (if false), so it will probably not achieve what you expect.
Use a == b && b == c to check if the value of the three objects are equal.
a == b == c is a comparison between c and result of a==b (1 or 0) operation.
use a==b&&b==c.
the condition a==b==c is equivalent to (a==b)==c which will provide the required result iff c==1, else the code will fail.

if(x==y==z) works, if(x!=y!=z) does not

Why is:
if(x!=y!=z)
handled as:
x=1
y=1
z=2
??
I just noticed it today.
x != y and x == y return booleans.
You're comparing z to those booleans.
Neither of them will work they way you want them to.
It probably is parsed as if ((x!=y) !=z) which does not do what you think if (x!=y!=z) should do (but does not).
Likewise if (x==y==z) probably means if ((x==y)==z) to the compiler which is not what you want.
Enable the warnings given by your compiler. With GCC, that means gcc -Wall and it would tell you warning: suggest parentheses around comparison in operand of '=='
Recall that a boolean expression like x==y gives a zero (when false) or non-zero (when true) result. Writing ((x==y) + (z==t)) is very poor taste, but makes sense for the compiler.
x == y == z is equivalent to (x == y) == z. In this case, (1 == 1) == 2, or true == 2, which is false because true == 1, not 2.
x != y != z is equivalent to (x != y) != z. In this case, (1 != 1) != 2, or false != 2, which is true because false == 0, not 2.
C(++) relational operators aren't chained like in Python. If you want to check whether three numbers are all equal to each other, use (x == y) && (y == z).
if(x==y==z)
will not work untill the value u will use for z be 1 or 0 but u can take the value of x and y anything
As when u try with the value 1 or 0 then the if will take its parameters as
if((x==y)==z)
this is happening because it first evaluate whatever the value in x and y and the answer will be in boolean and then it checks with z which it expects to be boolean. so if (x==y) be and z is 1(true) then code will be executed else it wont.Same thing will happen with (x!=y!=z). try with z=1 or 0 and x,y be anything.