For statements with for clause

May 30, 2024

Today we’ll look at the second, of three, types of for statements: Those with a for clause.

For statements with for clause

A “for” statement with a ForClause is also controlled by its condition, but additionally it may specify an init and a post statement, such as an assignment, an increment or decrement statement. The init statement may be a short variable declaration, but the post statement must not.

ForClause = [ InitStmt ] ";" [ Condition ] ";" [ PostStmt ] .
InitStmt = SimpleStmt .
PostStmt = SimpleStmt .
for i := 0; i < 10; i++ {
	f(i)
}

So a for statement with for clause differs from the simpler type we saw yesterday, the for statement with a single condition, in that it contains two additional statements. This works a lot like the simple statements we’ve already seen in if, switch, and select statements, with the addition of a post statement, not just an init statement.

If non-empty, the init statement is executed once before evaluating the condition for the first iteration; the post statement is executed after each execution of the block (and only if the block was executed).

Let’s look at the execution order here, in detail. Given a simple for loop like this:

for i := 0; i < 3; i++ {
	fmt.Println(i)
}

based on the above, the execution would look something like this, if broken out of a loop:

i := 0           // initial statement
if i < 3 {       // i == 0, 0 < 3 so execute the block
	fmt.Println(i) // Prints 0
	i++            // Post statement only executes if the block executes
}
if i < 3 {       // i == 1, 1 < 3 so execute the block
	fmt.Println(i) // Prints 1
	i++            // Post statement only executes if the block executes
}
if i < 3 {       // i == 2, 2 < 3 so execute the block
	fmt.Println(i) // Prints 2
	i++            // Post statement only executes if the block executes
}
if i < 3 {       // i == 3, 3 is not less than 3, so the block does not execute, and the loop ends
	/* not executed */
}

Quotes from The Go Programming Language Specification Language version go1.22 (Feb 6, 2024)


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Related Content


Omitting for clause elements

For statements with for clause … Any element of the ForClause may be empty but the semicolons are required unless there is only a condition. If the condition is absent, it is equivalent to the boolean value true. for cond { S() } is the same as for ; cond ; { S() } for { S() } is the same as for true { S() } So this goes to show that the single condition form of the for loop is really just a special case of the “for clause” for loop, with the init and post statements omitted.


Iteration over strings

For statements with range clause … For a string value, the “range” clause iterates over the Unicode code points in the string starting at byte index 0. On successive iterations, the index value will be the index of the first byte of successive UTF-8-encoded code points in the string, and the second value, of type rune, will be the value of the corresponding code point. If the iteration encounters an invalid UTF-8 sequence, the second value will be 0xFFFD, the Unicode replacement character, and the next iteration will advance a single byte in the string.


Iteration over arrays and slices

For statements with range clause … For an array, pointer to array, or slice value a, the index iteration values are produced in increasing order, starting at element index 0. If at most one iteration variable is present, the range loop produces iteration values from 0 up to len(a)-1 and does not index into the array or slice itself. For a nil slice, the number of iterations is 0. From this above paragraph, we can be assured that ranging over an array, pointer to array, or slice, will operate in a defined order–from index 0, upward.

Get daily content like this in your inbox!

Subscribe