Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

For loop syntax  RSS feed

 
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
Sheriff
Posts: 24216
37
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) ...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!