Why is this false? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why is this false?

int i = 5,j =7 k=9 j<5 || i <=7 && k==10;

31st Mar 2017, 2:07 AM
Sami
8 Answers
+ 18
@Samira Let x be true, x && !x true && !true true && false false Let x be false, x && !x false && !false false && true false This means that x && !x will always be false.
31st Mar 2017, 2:28 AM
Hatsy Rei
Hatsy Rei - avatar
+ 17
When we substitute the variables for their value instances: 7 < 5 || 5 <= 7 && 9 == 10 Remember that && has higher precedence than ||, hence: false || true && false false || false false
31st Mar 2017, 2:22 AM
Hatsy Rei
Hatsy Rei - avatar
+ 9
int x = 3; if(x && !x) cout << "true"; always be false 3 is true !3 is false So (true && false)
31st Mar 2017, 2:28 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 8
Because k is not 10, simple as that. && means AND || means OR AND I need k to be 10.
31st Mar 2017, 2:12 AM
Wen Qin
Wen Qin - avatar
+ 8
Ah......really? I think it was j<5 make it return false After I flip it into j>5 condition return true
31st Mar 2017, 2:15 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 5
AND operator has a higher precedence than OR operator http://en.cppreference.com/w/cpp/language/operator_precedence j(7)<5 || (i(5) <= 7 && k(9) == 10) j(7)<5 || (true && false) false || false false
31st Mar 2017, 2:23 AM
Heng Jun Xi
Heng Jun Xi - avatar
+ 1
Thank you all so much for helping!! Why would something like this: x && !x be false then??
31st Mar 2017, 2:26 AM
Sami
+ 1
Thank you all soo much! That was very helpful, I appreciate it :D
31st Mar 2017, 2:30 AM
Sami