Reputation: 9877
I'm looping through my view
's subviews
(with a total of 10 subviews).
Before doing so, I declare a new int
to be used as a counter.
I'm checking if the count isn't equal to a hard-coded value (!=
):
int count = 0;
for (UIView *subview in self.view.subviews) {
count = count + 1;
if (count != 5) {
NSLog(@"Hello");
} else {
NSLog(@"World");
}
}
With this, NSLog(@"World");
is invoked when the count is equal to "5" (as expected):
Hello
Hello
Hello
Hello
World
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Now to the issue, if I add "or pipes" (||
), NSLog(@"World");
is never invoked (if
isn't validated).
int count = 0;
for (UIView *subview in self.view.subviews) {
count = count + 1;
if (count != 5 || count != 6) {
NSLog(@"Hello");
} else {
NSLog(@"World");
}
}
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Now this issue can ofc be fixed by changing the syntax, but my question is why isn't the if
validated?
It should be noted, that changing the "not equal" (!=
) to "is equal" (==
) solves the issue.
Is this a bug, or have I missed something?
Upvotes: 0
Views: 35
Reputation: 410622
Your conditional is always true. Given count != 5 || count != 6
, if count == 5
then the latter half is true; if count == 6
then the former half is true; and if count equals anything else, the statement is also true.
You may have meant to use &&
instead.
Upvotes: 3