Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

For loop syntax

 
Patrick Sylvester-Jose
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Patrick Sylvester-Jose
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
test.java:12: <identifier> expected
for (int i = 0, int j = 1; i < 4; i++)
^
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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) ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic