+ 1

Why the answer to this question is not true?

int x = 3; int y = 8; if(!(x > 2 || y < 0)) printf("true"); else printf("false"); //y is 8. 8 is not less than 0, so this expression is true. If one of the expression is true, then why the answer is not "true"?

25th Dec 2018, 5:57 AM
Konybud
Konybud - avatar
6 Respuestas
+ 16
● lets see the 1stexpression : (x > 2 || y < 0) Here, 1st condition (x>2) is true, so result of whole expression will be true only. ● now putting ! on it, ie !(x > 2 || y < 0) => !(true) => false //have a look at this post also [Little DIFFERENT] https://www.sololearn.com/Discuss/1632203/?ref=app
25th Dec 2018, 6:11 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 16
if any of the condition will be true (connected by || operator) it will return true only
25th Dec 2018, 6:17 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 5
STUDENT Piyakon (Kon) Cheung Maybe you're thinking too complicated. It will evaluate the expression (x > 2 || y < 0) first. x > 2 is true, so it won't even check if y < 0 is true or false because if one condition is true, the whole expression is true. In the next step, the result is negated because of the !(). So you get the negation of true which is false.
25th Dec 2018, 6:37 AM
Anna
Anna - avatar
+ 1
So it will read the first expression first?
25th Dec 2018, 6:14 AM
Konybud
Konybud - avatar
0
3>2.So it's true and ! makes it false.
2nd Jun 2020, 8:54 PM
dia
dia - avatar
0
x>2 - true y<0 - false (x>2 || y<0) - true since its logical OR operator !(true) - false * if statements are executed only if the the expression or condition of 'if' is true . the code becomes.. if(false) printf("true"); else printf("false"); now, since the condition inside if is false, it will execute the else statement which is 'false' hence, it prints false..
14th Aug 2020, 4:41 PM
Jisha Joseph
Jisha Joseph - avatar