I think it's wierd that i have to say: if x and y is 0 then no division.. |
On the contrary, if you want no division to occur when either of x or y is 0, you must not divide
if x or y is 0.
You use && when you want to evaluate whether "condition a is true and condition b is true". You use || when you want to evaluate whether "condition a is true or condition b is true."
That's why for me it's much more logical to use || (or), because then it doesn't devides if just 1 of them is 0.. |
You used or with inequality which, not surprisingly, leads to different results than using or with equality.
The original code you supplied (well, at least the second snippet) looks like:
1 2 3 4
|
if(y != 0 || x != 0)
// do division
else
// don't do division
|
In English: If y is not 0
or if x is not 0, do division. The problem there of course is that x can be not 0 while y is 0, and the condition evaluates to true, because that's what we asked for.
Consider what the if statements look like for particular values of x and y when the expressions are replaced with the true or false value they evaluate to:
x = 0, y = 0
if ( y!=0 || x!=0 ) ----> if ( false || false ) ----> false
x=1, y=0
if ( y!=0 || x!=0 ) ----> if ( false || true ) ----> true
x=0, y=1
if ( y!=0 || x!=0 ) ----> if ( true || false ) ----> true
x=1, y=1
if ( y!=0 || x!=0 ) ----> if ( true || true ) ----> true |