+ 1

is 2==x wrong ?? if so why?

17th Jul 2018, 5:50 PM
rj.paul
rj.paul - avatar
13 Answers
+ 4
The x variable must be either one of the integral types or floating point types, then both forms of comparison ( 2 == x and/or x == 2 ) gets evaluted, otherwise it's a compilation error due to the type mismatch. ■■ Some examples ■■ char x1 = '2'; int x2 = 2; long long x3 = 2LL; double x4 = 2.0; string x5 = "2"; int *x6 = nullptr; // Check 2 against each type bool b1 = 2 == x1; // legal - false bool b2 = 2 == x2; // legal - true bool b3 = 2 == x3; // legal - true bool b4 = 2 == x4; // legal - true bool b5 = 2 == x5; // illegal - type mismatch - error bool b6 = 2 == x6; // illegal - type mismatch - error
17th Jul 2018, 8:38 PM
Babak
Babak - avatar
+ 2
https://code.sololearn.com/W2pnqLaDqG6c/?ref=app I knew it was not wrong but took 30 seconds to try it anyway. It's not wrong at all, since it compares values with values. P.S. SoloLearn has a code playground you can use to test these fun experiments yourself 😊
17th Jul 2018, 5:59 PM
Andre Daniel
Andre Daniel - avatar
+ 2
cause 2 is a number ... and it isn't proper to start with a number.. or symbol... I think...
17th Jul 2018, 6:01 PM
aderinola gbenga
aderinola gbenga - avatar
+ 2
It's basically a value==value in that expression. So it's right either way.
17th Jul 2018, 6:03 PM
Andre Daniel
Andre Daniel - avatar
+ 2
secondly 2 and x can't be equal... .... ...😀
17th Jul 2018, 6:03 PM
aderinola gbenga
aderinola gbenga - avatar
+ 2
aderinola gbenga if x has the value of 2 it will return true, if not, it will return false. The user was asking if the expression is possible, which the answer is yes. I think you misinterpreted the question being asked.
17th Jul 2018, 6:04 PM
Andre Daniel
Andre Daniel - avatar
+ 2
OK then👍
17th Jul 2018, 6:14 PM
aderinola gbenga
aderinola gbenga - avatar
+ 1
aderinola gbenga its not wrong, and there are no rules against it.
17th Jul 2018, 6:01 PM
Andre Daniel
Andre Daniel - avatar
+ 1
ok, thanks Andre Daniel👍
17th Jul 2018, 6:03 PM
rj.paul
rj.paul - avatar
0
What are you referring to? I am confused.
17th Jul 2018, 5:53 PM
Andre Daniel
Andre Daniel - avatar
0
if I had to check a value of x using '==', is the above method wrong?
17th Jul 2018, 5:56 PM
rj.paul
rj.paul - avatar
0
I did try it on my own but I thought it 2==x would be approached differently by the computer
17th Jul 2018, 6:02 PM
rj.paul
rj.paul - avatar
0
thanks c++ soldier 👍👍
18th Jul 2018, 9:41 AM
rj.paul
rj.paul - avatar