SkyeBoniwell
SkyeBoniwell

Reputation: 7092

checking multiple conditions in an if else block

This seems like it should be simple, but this block of code is not working.

I just need to check and make sure the current race_position is not equal to the total count or not greater than the count or not equal to 0. If it's any of those, then I want to break out of the loop.

I even went through it in the visual studio debugger, and saw when race_position was 0, it still acted as if it was not 0 and never broke out of the loop.

Is there a way to simply this and make it work?

Thanks!

if (race_position != race_team.Count || !(race_position > events.Count) || race_position != 0)
{
    next_position = (race_position + 1);
}
else
{
    break;
}

Upvotes: 1

Views: 105

Answers (2)

mybirthname
mybirthname

Reputation: 18127

if(!(race_position < race_team.Count &&  race_position > 0))
    break;

next_position = (race_position + 1);

Upvotes: 2

Jonesopolis
Jonesopolis

Reputation: 25370

you are ORing (||) the conditions together, meaning if one is true, then that block will execute. You want to AND (&&) them together, so that they all must be true, or else the loop will exit.

when race_position is 0, then race_position != race_team.Count is true, hence your loop continues.

Upvotes: 5

Related Questions