• 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 initialization clause

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks

Looking at for loops today and had a question.

I know you can define a var prior to a for loop and init it in the initialization part of the for loop. And you can declare and init a var in the initialization part of a for loop. And you can do this for multiple vars.

Some test code I've written implies the vars MUST be all the same type AND you cannot mix prior declared vars with vars being declared in the loop.

Is this conclusion correct?

Mark
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The two conclusions that you've reached are correct. However you can do this

Basically here you are using two variables of different types as they are already declared...
 
Ranch Hand
Posts: 53
MyEclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
send the clipboard of some test code case
 
mark juszczec
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

maganti suryanarayana wrote:send the clipboard of some test code case





all the above compiles fine EXCEPT for the last line which gives the following errors:

javac ForLoopTest.java
ForLoopTest.java:20: '.class' expected
for (j=0, int z; j < 5; j++);
^
ForLoopTest.java:20: not a statement
for (j=0, int z; j < 5; j++);
^
ForLoopTest.java:20: not a statement
for (j=0, int z; j < 5; j++);
^
ForLoopTest.java:20: ')' expected
for (j=0, int z; j < 5; j++);
^
ForLoopTest.java:20: ';' expected
for (j=0, int z; j < 5; j++);
^
5 errors

 
mark juszczec
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:The two conclusions that you've reached are correct. However you can do this

Basically here you are using two variables of different types as they are already declared...



So doesn't this mean that only one of my conclusions that is true is: "you cannot mix prior declared vars with vars being declared in the loop" ?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic