Heyho,
can anybody explain me in which order the operators or in which ‘blocks’ would check an IF-Statement?
For Example: I want to check if one player has two more points than the other AND the current round is greater then 3.
if (player1WinCount == player2WinCount + 2 || player2WinCount == player1WinCount + 2 && roundNumber >= 3)
I’m not sure if this would work. Does this code check if one player has two more wins and THEN checks if the roundNumber is greater-equal 3 or do I have to put the ‘&& roundNumber >= 3’ on both sides of the OR (||) operator? Like this:
if (player1WinCount == player2WinCount + 2 && roundNumber >= 3 || player2WinCount == player1WinCount + 2 && roundNumber >= 3)
Conditions get processed first to last, but keep in mind that you can often avoid duplicate in logic statements, for example in your case, you can do the OR operation first on the players win counts, and then check the round number, like so.
if ((player1WinCount == player2WinCount + 2 || player2WinCount == player1WinCount + 2) && roundNumber >= 3)
Additionally, if you ever get a bit confused with larger combinations of conditional checks, you can just break them up to make things clearer to read, the cost of a few extra booleans really wont hit you that hard at all. so you could do something like this as well.
bool isThirdRoundOrMore = (roundNumber>= 3);
bool isOnePlayerTwoAhead = (player1WinCount == player2WinCount + 2 || player2WinCount == player1WinCount + 2);
if (isThirdRoundOrMore && isOnePlayerTwoAhead)