Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

variable declaration in init statement of for loop

Tags:

for-loop

go

Yet another beginner's question for golang:

I can write:

for i := 0; i < 10; i++ {}

But if I want i to be of a specific type as int64, apparently I am not supposed to write:

for var i int64 = 0; i < 10; i++ {}

I am surprised that I cannot specify a type in the start statement of the for loop. I need to write something like this:

var i int64
for i = 0; i < 10; i++ {}

Am I missing a point here? Is there any explanation for it?

like image 420
linqu Avatar asked Jan 15 '15 09:01

linqu


People also ask

Can you declare a variable in a for loop?

Often the variable that controls a for loop is needed only for the purposes of the loop and is not used elsewhere. When this is the case, it is possible to declare the variable inside the initialization portion of the for.

How do you initialize a for loop?

Here, initialization sets the loop control variable to an initial value. Condition is an expression that is tested each time the loop repeats. As long as condition is true, the loop keeps running.

Can we initialize variable in for loop in Python?

Answer. In Python, a for loop variable does not have to be initialized beforehand. This is because of how Python for loops work.

Can we initialize in for loop?

In Java, multiple variables can be initialized in the initialization block of for loop regardless of whether you use it in the loop or not. Example: Java.


2 Answers

The language specification for a for loop states that: The init statement may be a short variable declaration, which is assignment of the form i := 0 but not a declaration of the form var i = 0. As for the reason behind this - I'm guessing language simplicity. See here: http://golang.org/ref/spec#For_statements

BTW You can do something like this:

for  i := int64(0); i < 10; i++ {
   // i here is of type int64
}
like image 84
Not_a_Golfer Avatar answered Sep 30 '22 04:09

Not_a_Golfer


According to the Go Language Specification: For statements if you choose the form of the ForClause:

ForClause = [ InitStmt ] ";" [ Condition ] ";" [ PostStmt ] .

The definition of the InitStmt:

SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt | Assignment
               | ShortVarDecl .

So as you can see if you want to create a new variable in the for statement, it can only be a Short Variable declarations, like this:

for i := 0; i < 10; i++ {}

The Short variable declaration can be used to declare new variables of any types, and the types of the variables will be the types of of the corresponding initialization values on the right side.

Let's examine it a little bit closer:

i := 0

On the right side there is 0: it is an untyped numeric constant. Note that untyped consants in Go have a default type which is used if a type is needed, like this case. The default type of an untyped integer consant is int hence the variable i will have int type.

If you want it to have the type int64, use an expression which has int64 type, like these:

for i := int64(0); i < 10; i++ {} // Explicit type conversion

const ZERO int64 = 0
for i := ZERO; i < 10; i++ {}     // Typed constant

var vzero int64

for i: = vzero; i < 10; i++ {}    // Expression of type int64

Or create the i variable before the for statement using var declaration:

var i int64
for i = 0; i < 10; i++ {}         // Variable is declared before for using "var"

Note that this last example does not use the Short Variable declaration just a simple assignment.

like image 23
icza Avatar answered Sep 30 '22 06:09

icza