aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes For loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "For loop" Watch "For loop" New topic
Author

For loop

Kaz Yosh
Ranch Hand

Joined: May 22, 2003
Posts: 63
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.


"If anything can go wrong, it will"
Lawrence Chettiar
Ranch Hand

Joined: Apr 10, 2003
Posts: 62
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


SCJP 1.4 (86%)<br />SCMAD Beta (77%)<br />SCEA (part I:89%,partII:93%)<br />MCAD (cleared 70-315 & 70-320 [Score 980])
Kaz Yosh
Ranch Hand

Joined: May 22, 2003
Posts: 63
I still dont know why the first one is valid and the rest is invalid.
G Nadeem
Ranch Hand

Joined: Apr 25, 2003
Posts: 48
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

Joined: Apr 10, 2003
Posts: 62
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.
Brett Turner
Greenhorn

Joined: May 15, 2003
Posts: 6
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;
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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 ]

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Kaz Yosh
Ranch Hand

Joined: May 22, 2003
Posts: 63
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.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Mar 05, 2003
Posts: 1391
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

Joined: May 22, 2003
Posts: 63
Thank you for detailed explanation.
I understood
Thomas De Vos
stable boy
Ranch Hand

Joined: Apr 12, 2003
Posts: 425
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


Try your free <a href="http://www.javacertificate.com" target="_blank" rel="nofollow">SCJP 1.4</a> certification centre.<br />Try your free <a href="http://www.j2eecertificate.com" target="_blank" rel="nofollow">SCWCD</a> certification centre.<br />Try your free <a href="http://www.ejbcertificate.com" target="_blank" rel="nofollow">SCBCD</a> certification centre.<br />Try your <a href="http://www.webspherecertificate.com" target="_blank" rel="nofollow">Websphere (Test 285) </a> certification centre.<br />Try your <a href="http://www.j2mecertificate.com" target="_blank" rel="nofollow">SCMAD</a> certification centre. (New)<br /> <br /><a href="http://blogs.javacertificate.com" target="_blank" rel="nofollow">Java/J2EE Certification Blogging</a>
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Jul 02, 2002
Posts: 1865
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: For loop
 
Similar Threads
Urgent Help needed
While loop never stops
for loop: Faster ( and more elegant ?)
let me know about the logic here
I/O problem