joffd joffd -5 years ago 152
Objective-C Question

If && not working

I have looked at every similar question i can find but i am scratching to work out why my if && stament isn't working.

if ((cEventStatus) && (ddayDif = 0)) {
[self upDateCountersWorking];

}else if ((cEventStatus) && (ddayDif > 0 )){
[self currentlyWorkingAddDays:&ddayDif];

}else if ((!cEventStatus) && (ddayDif = 0)){
[self upDateCountersResting];

}else if ((!cEventStatus) && (ddayDif > 0)){
[self currentlyRestingAddDays:&ddayDif];
NSLog(@"final else");

I originally didn't have enough () around each statement and only & instead of &&, from the other questions i found i made the changes to come to the above code. But still no success.

In desperation i have added the following code to confirm the variables and am getting the expected results.

if (cEventStatus) {
NSLog(@"YES status");
NSLog(@"NO status");

if (ddayDif > 0) {
NSLog(@"greater than 0");
NSLog(@"less than 0");

But the if else never gives the expected result, always falls to the final else regardless of variable values.

I am sure this will be a simple issue but any help is appreciated.

Answer Source

This line:

if ((cEventStatus) && (ddayDif = 0)) {

isn't checking the value of ddayDif, it's setting it (to zero) - which isn't what you want.

In each of your checks you should be using == to test the value (instead of = to set it).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download