File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes For loop syntax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "For loop syntax" Watch "For loop syntax" New topic

For loop syntax

Patrick Sylvester-Jose

Joined: Apr 10, 2005
Posts: 6
In the initialization part of the for loop, is it possible to declare more than 1 variable? I can't seem to find any definitive answer.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
That kinda rings a bell. Try two clauses with commas between them and see if it compiles ...

for( int i = 1, int j=1 ; etc

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Patrick Sylvester-Jose

Joined: Apr 10, 2005
Posts: 6 <identifier> expected
for (int i = 0, int j = 1; i < 4; i++)
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

The two variables have to be the same type, because they have to share the same type declaration:

for (int i=0, j=0; i<10; ++i, ++j) ...

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: For loop syntax
It's not a secret anymore!