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!
5 ответов
+ 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.
+ 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.
+ 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
+ 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.
0
Got it! Thank you very much!