Syntax for a loop without braces?

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 the loop does not have curly braces, and the next line is not even an expression with ; . This is just a for loop.

What's up with that?

+4
source share
6 answers

Insert trailing expressions in braces. If only one statement should be included in a loop, curly braces can be omitted.

Opening and closing braces for if, for, or while statements should always be used, even if the body of the statement contains only one statement.

Braces improve code uniformity and readability. More importantly, when you insert an additional statement into a body containing only one statement, it is easy to forget to add curly braces, because the indentation gives a strong (but misleading) guidance to the structure.

 for(int i = 0; i < 10; ++i) { Console.WriteLine(i) } 

NOTE : after the cycle. Without curly braces, the loop will contain only the first statement immediately after the loop statement.

see this for more information: http://www.dotnetperls.com/omit-curly-brackets

+1
source

MSDN : a for loop executes a statement or statement block several times until the specified expression evaluates to false.

The main thing is to understand what the operator or block of operators performs . The nested for in your example is an operator that contains a block of statements because of the { } pair.

So, if you wrote above, as soon as there is one statement for each 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 statement for each 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); } } } 
+6
source

The body of a for loop without curly braces is just the next statement. In this case, the second for loop is a statement that the body of the first.

The grammar in the C # 4.0 specification (sections 8 and 8.8.3) looks like this:

 for-statement: for ( for-initializer; for-condition; for-iterator) embedded-statement embedded-statement: block empty-statement expression-statement selection-statement iteration-statement jump-statement try-statement checked-statement unchecked-statement lock-statement using-statement yield-statement 

Thus, the body of the for loop is defined as embedded-statement . When you see braces around the body of the loop, a block , which is the first option for embedded-statement . Having another for loop as an embedded-statement is one of the options that apply to iteration-statement (section 8.8).

+4
source

The first statement for the loop is the second statement for the loop, so your program works well with your syntax, even if you also write your own syntax:

 for (int y = 0; y < GameBoard.GameBoardHeight; y++) for (int x = 0; x < GameBoard.GameBoardWidth; x++) if (GetSquare(x, y) == "Empty") RandomPiece(x, y); 
+3
source

A for The body of a loop can contain instructions (for example, loops, conditions) without curly braces.

+2
source
Cycle

A for also an expression. So this is legal C # code. Control flow constructs (at least inherited from C) can have either one operator or a block of several operators:

 for (...) statement for (...) { statement* } 
+2
source

All Articles