for (int y = 0; y < GameBoard.GameBoardHeight; y++)
for (int x = 0; x < GameBoard.GameBoardWidth; x++)
{
if (GetSquare(x, y) == "Empty")
{
RandomPiece(x, y);
}
}
The first for loop has no braces, and the next line is not even a statement with a ;
. It's just a for
loop.
Whats up with this?
MSDN: The for loop executes a statement or a block of statements repeatedly until a specified expression evaluates to false.
The main point to understand is the executes a statement or a block of statements part. The nested for
in you example is a statement which contains a block of statements because of the { }
pair.
So, if you wrote the above as only a single statement per nested operation, you will write:
for (int y = 0; y < GameBoard.GameBoardHeight; y++)
for (int x = 0; x < GameBoard.GameBoardWidth; x++)
if (GetSquare(x, y) == "Empty")
RandomPiece(x, y);
or as a block statements per nested operation:
for (int y = 0; y < GameBoard.GameBoardHeight; y++)
{
for (int x = 0; x < GameBoard.GameBoardWidth; x++)
{
if (GetSquare(x, y) == "Empty")
{
RandomPiece(x, y);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With