2

I've done some searches and couldn't find a list of valid types to use in for loop initialization statements. Is there a fixed list of types that can be used in for loop variable declarations? For instance, consider the following code:

for (int i = 0; i < 5; i++) // ...
for (String str = "a"; str.length() < 10; str+="a") // ...

The first works, but I don't think the second will. Is there a list of all the types which are permitted in a for loop initialization?

7
  • 3
    Have you tried the second? You can use whatever type you want, there's no restrictions.. you can even initialize multiple variables, as long as they're all the same type. Commented Oct 13, 2013 at 2:34
  • Oh, wow, I was told by my lecturer that there were restrictions on types in for loops... didn't bother to test it myself. Sorry Commented Oct 13, 2013 at 2:42
  • 1
    Ah, well, your lecturer should have a look at the the for statement in the Java language specification, then. :) There are restrictions on what can be there; either multiple statements separated by commas, or a declaration of one or more variables, and the condition expression has to be boolean, but there's no restriction on the types of variables that can be declared. Commented Oct 13, 2013 at 2:52
  • A mod can delete this question if they want, or simply leave it Commented Oct 13, 2013 at 3:00
  • I don't think it's a terrible question. You had a valid question, and and showed examples of what you knew would work and what you suspected wouldn't, and clarified in the comments why you thought it wouldn't work. Maybe there are languages out there that do restrict the type of iteration variables you can use. If someone's coming from one of those languages, this could help them. Commented Oct 13, 2013 at 3:04

2 Answers 2

4

Have a look at the Java language specification for the for statement. You can declare and initialize any type of variable in a for loop, and can even declare multiple variables, so long as they're all the same type. The relevant productions in the grammar are:

BasicForStatement:
    for ( ForInitopt ; Expressionopt ; ForUpdateopt ) Statement

ForInit:
    StatementExpressionList
    LocalVariableDeclaration

LocalVariableDeclaration:
    VariableModifiersopt Type VariableDeclarators

VariableDeclarators:
    VariableDeclarator
    VariableDeclarators , VariableDeclarator

This means that you can do any of the following, e.g.,

for ( ; … ; … )                         // no variable declaration at all
for ( int i; … ; … )                    // variable declaration with no initial value
for ( int i=0; … ; … )                  // variable declaration with initial value
for ( int i=0, j=1; … ; … )             // multiple variables
for ( final Iterator<T> it = …; … ; … ) // final variable

The first example there shows that you don't need any variables at all, and as pointed out in the comments, you don't have to have a ForUpdate either. The only constraint is that you must have an expression in the middle, and it needs to be a boolean valued expression.

As an aside, the ForInit can also be a StatementExpressionList, which means that instead of declaring and initializing variables, you can also just execute some statements. E.g, you could do this (but this isn't a particularly useful example):

for ( System.out.println( "beginning loop" ; … ; … )

This could be useful, I suppose, in simulating a do/while loop (if you wanted to do that), if the body is a simple function call:

for ( method() ; condition ; method() );
Sign up to request clarification or add additional context in comments.

3 Comments

While he's working on it; the short answer is that the middle (the Expressionopt) has to evaluate to a boolean or a Boolean, but there's no such restriction on the types for other two parts.
@DennisMeng That's a good point. I don't think it's exactly what OP's asking, but it's a good point. The other two parts don't even have to be present!
In particular, for(;true;); should be valid.
1

Second one also will work fine. You can use any type for for loop

 for(String str="a";str.length()<10;str+="a")
    {
        System.out.println(str);
    }

I just tried for your scenario and result is

a
aa
aaa
aaaa
aaaaa
aaaaaa
aaaaaaa
aaaaaaaa
aaaaaaaaa

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.