Reputation:
Let us say I have an integer array representing the chess pieces on a board;
int board[8][8];
In my chess game, I am currently coding a generator function that will return an integer vector of all legal moves.
Naturally, I will be using if statements
I am at a point where I need to check a certain element in the board relative to a piece on the board
For example, If I have a pawn piece;
board[row][col] == 'p';
I need to generate [row+1][col]
, [row+2][col]
and in some cases if it can attack a piece, a change in column too.
But if a piece is on any edge of the board
, board[row+1][col]
will return be index out of range
For that reason I need an extra if statement.
My question is, shall i use:
if (pieceisnotonedge && board[row+1][col] == 0)
or
if (pieceisnotonedge)
{
if (board[row+1][col] == 0)
}
For the first example, if pieceisnotonedge
returns false
, will it also check the next condition? Because if it does, then I am in trouble.
Upvotes: 0
Views: 2379
Reputation: 40872
For the shown code there is no difference, both board[row+1][col] == 0
are only evaluated if pieceisnotonedge
is true
.
If you should use one or the other, cannot be said be in general, as it depends on other factors (like semantic, readability, …) too.
If you e.g. want to have an action that it done if either pieceisnotonedge
or board[row+1][col] == 0
is false, then you probably would use &&
, because then you can write:
if (pieceisnotonedge && board[row+1][col] == 0) {
} else {
// do something else
}
with your other style you would need to write:
if (pieceisnotonedge){
if (board[row+1][col] == 0) {
// do something else
}
} else {
// do something else
}
In general, your first aim should be to have readable code. While pieceisnotonedge && board[row+1][col] == 0
easy to read, more complex expressions might not be. So you would decide that on a case by case basis, if you want to use one expression or multiple if
.
Upvotes: 2
Reputation: 9366
For the first example, if pieceisnotonedge returns false, will it also check the next condition?
No. It will "short-circuit" because if the first condition is false, checking the conditions after it is unnecessary. Read more here and here.
This is guranteed by the C++ standard:
7.6.14
... && guarantees left-to-right evaluation: the second operand is not evaluated if the first operand is false.
Note that, for ||
this is opposite, that is, if the first condition is "true", then checking the conditions afterwards is unnecessary
Shall i use; or...
Both are same, if you have a short if statement (with only two conditions), I would suggest using the first approach. In terms of efficiency there is no difference and you can verify this by looking at the generated assembly for both cases on godbolt
Upvotes: 2
Reputation: 32952
For the first example, if
pieceisnotonedge
returnsfalse
, will it also check the next condition?
No, it will not. Because the build-in logical operators do short-circuiting. From cppreference.com:
Builtin operators
&&
and||
perform short-circuit evaluation (do not evaluate the second operand if the result is known after evaluating the first), but overloaded operators behave like a regular function calls and always evaluate both operands
Therefore, in
if (pieceisnotonedge && board[row+1][col] == 0)
if the pieceisnotonedge
is false
, the second will not be evaluated. Therefore, having a nested if
s is redundant, and you can go with the first version.
Upvotes: 5
Reputation: 17698
For the first example, if pieceisnotonedge returns false, will it also check the next condition?
No. It will stop immediately after pieceisnotonedge
is evaluated to false. There is no subsequent check for the remainder condition board[row+1][col] == 0
You can use nested if
as the second code as well - no difference. It's just a matter of what code would look clearer.
Upvotes: 1