0

How to understand it?

int a=2000; if (a%4==0 && a%100!=0 II a%400==0) printf("yes"); ---- Output: yes ---- How does computer work? I think if "&&" is equal to "ll", cmputer should be work from left to right. So, when it goes to "&& a%100!=0", it will return false. But the output is "yes". How to understand it? Thank you!

5th Jan 2019, 11:39 PM
Eason Huang
Eason Huang - avatar
5 odpowiedzi
+ 2
&& in most programs has higher priority to || But that's actually not your question. Even if you just evaluate left to right you have 1 && 0 || 1 = 0 || 1 = 1. Nothing wrong with that.
5th Jan 2019, 11:47 PM
ChSp
ChSp - avatar
+ 2
The keyword here is 'operator precedence'. And since this is different between languages, you gotta google that table for your language and swallow that information.
6th Jan 2019, 12:25 AM
HonFu
HonFu - avatar
+ 1
man, the pipe-lines - these two pipes "||" - means "or" and not means "and" like "&&" so when you put "... || a%400==0" in anyplace in a code, you made the all rest of condition previous or next will be TRUE(be cause 2000 % 400 = 0 it's true), like it: //code: if(false || true){ // ... } it ever be true, be cause some condition is true: && : All conditions needed be true to it be true || : just only one condition needed true to it be true I hope to have help you sry, my english is a shit
5th Jan 2019, 11:50 PM
Davi Mendes Pimentel
Davi Mendes Pimentel - avatar
+ 1
The ‚if‘ condition is evaluated in its entirety before jumping to the result. So while (a%4 == 0 && a%100 != 0) would result in a FALSE, the condition a%400 == 0 is TRUE. The OR connection between the former and the latter part means the overall result is TRUE.
5th Jan 2019, 11:53 PM
Alex
0
Got it! Thank you very much!
5th Jan 2019, 11:50 PM
Eason Huang
Eason Huang - avatar