• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

For loop

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I remember that for loop doent accept more than one variable initialization. If so,
Is this valid?
3.� for (int i=0, j=0; ; i++) { System.out.println("Blake"); }
This is from mock exam on javacertificate.com.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope you can use more then one intitilizer in a for loop the only traps the exams(I mean mock ones I have yet to give the actual exam) has is
int j;
for (int i=0, j=0; ; i++)
{ System.out.println("Blake"); }
in this case the j is already defined and will give compiler error.
also
for (int i=0, int j=0; ; i++)
{ System.out.println("Blake"); }
is invalid
for (int i=0, long j=0; ; i++)
{ System.out.println("Blake"); }
and this is invalid too
Hope it helped
Lawrence
 
Kaz Yosh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still dont know why the first one is valid and the rest is invalid.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kaz,
(code taken from Lawrence's example)
1)
Here both i and j are being declared and initialised as int. hence valid.


Here, as Lawrence explained, declaration of variable i as int is being mixed up with initialisation of j. the line inside loop initialisation, int i =0,j=0 is both declaration and initialisation of both variables i and j as integer. as j is already declared, compiler rejects it. (so cant mix initialisation with declaration).

this rejection is a consequence of another rule for 'for' loop. cant mix declaration/initialisation of variables of different primitive type. i.e all variables can either be int, or byte etc. but not int with byte, byte with long, int with long etc.
hope it helps.
 
Lawrence Chettiar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks nadeem for the explanation.
one important point in my example 3 the code fails not becoz of mixture of declartion but coz you can only have one declartion in a for loop
i.e
even this will give compiler error
for (int i=0, int j=0; ; i++)
{ System.out.println("Blake"); }
which as per your explanation should compile successfully.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lawrence Chettiar:
[QB]
for (int i=0, int j=0; ; i++)
{ System.out.println("Blake"); }
QB]


You can never have 2 statements separated by a comma, only by semi-colons.
In declarations, you would never use:
int i=0, int j=0;
you would just use
int i=0,j=0;
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Opps. Thomas and Marlene later pointed out that everything here is indeed documented in the JLS. Please disregard everything in this post.
I know that questions such as these appear on mock exams, but I did not see anything like this on the real exam. Of course, everyone gets a different exam so my experience doesn't limit the types of questions that others might see.
I agree that the for-loop behaves as described in this thread, but I'm not yet convinced that the real exam includes questions such as those described above. The reason for my skepticism is the fact that I don't see this behavior documented in the Java Language Specification. Since the real exam focuses on knowledge of the JLS rather than knowledge of actual behavior it seems unlikely that the real exam would test your knowledge of behavior that is not defined in the JLS.
Has anyone found a description of this behavior in the JLS? I don't see it in section 14.13. Did I miss something?
I agree that a programmer should be familiar with the actual behavior of Java so I think it is important to know the information described earlier in this thread. I'm just curious to know if anyone has actually seen a question like the above on the real exam.
[ May 27, 2003: Message edited by: Dan Chisholm ]
 
Kaz Yosh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me make sure I understand this.
int j;
for (int i=0, j=0; ; i++)
{ System.out.println("Blake"); }
this code is invalid because variable j is declared twice.
once outside the for loop and once inside the for loop.
am I right?
It seems to me that it is a question of variable declaration rather than for loop.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
am I right? Yes.
int j;
for (int i=0, j=0; ; i++)
int j is a local variable declaration.
int i = 0, j = 0 is also a local variable declaration. (by JLS 14.13)
The name of the local variable j may not be redeclared as a local variable within the scope of j, or a compile-time error occurs. (by JLS 14.4.2)
The scope of j is the rest of the block in which the declaration appears. (by JLS 14.4.2)
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for (int i=0, j=0; ; i++)
int i = 0, j = 0 is a local variable declaration.
A local variable declaration has the form
final(optional) Type VariableDeclarator , ... , VariableDeclarator
Only one Type applies to all the VariableDeclarators. Thus, int applies to both i and j.
Each VariableDeclarator has an Identifier and optional []'s and optional = Initializer. In your example i=0 and j=0
The VariableDeclarators are separated by commas.
[ May 27, 2003: Message edited by: Marlene Miller ]
 
Kaz Yosh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for detailed explanation.
I understood
 
stable boy
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dan,
The question was based on the Java Tutorial: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html
The language spec says indeed not which so much words the example given above. But the following is mentioned:
ForStatement:
for ( ForInitopt ; Expressionopt ; ForUpdateopt )
Statement
Where the ForInit is referred back to section 14.8, this is where the intialisation statement expressions values are mentioned.
Thomas De Vos
http://www.javacertificate.com
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your replies Thomas and Marlene.
I know that you can't do something like the following.
for (int i=0, int j=0; i<10;j++,i++) {}
I must have incorrectly extended that limitation to the following.
for (int i=0, j=0; i<10;j++,i++) {}
Now I know why that incorrect extention is not documented in the JLS.
The first is illegal because it is a comma separated list of assignment statements (int i=0, int j=0) while the second is legal because it is a comma separated list of assignment StatementExpressions (int i=0, j=0).
I'll have to add some questions on the ForInit expression to my mock exam. Currently, it focuses on things that have an impact on loop iteration but not much on initialization.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

a comma separated list of assignment statements (int i=0, int j=0)

In my opinion, int i=0 and int j=0 are not assignment statements. An assignment statement does not include a type. I would call each one a LocalVariableDeclaration.
It is illegal because the ForInit allows only *one* LocalVariableDeclaration.

a comma separated list of assignment StatementExpressions (int i=0, j=0)

In my opinion, i=0 and j=0 are not assignment StatementExpressions, although they look the same as assignment StatementExpressions. Because they are preceded by a type, they are VariableDeclarators.
Also, the ForInit is *either* a comma separated list of StatementExpressions *or* *one* LocalVariableDeclaration. Since int i=0, j=0 is a LocalVariableDeclaration, it is dangerous to call i=0 and j=0 assignment StatementExpressions.

I'll have to add some questions on the ForInit expression to my mock exam.

That�s probably a good idea, because Java is a little different from C on this issue, isn�t it? Gosh, I�ve forgotten C!
[ May 27, 2003: Message edited by: Marlene Miller ]
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marlene,
Yes, your terms are more precise. Originally I just wrote that one code example declared the variables using Statements while the other used StatementExpressions. Later I tossed in the word "assignment" just to be a little more clear. I didn't use the word "declaration" because Java does not define a "Declaration" StatementExpression. The basic idea here is that a StatementExpression is something that can be either a statement or an expression and can be used in a comma separated list such as a StatementExpressionList.
 
reply
    Bookmark Topic Watch Topic
  • New Topic