Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

for loop.. is this legal?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is actually from JavaRanch ...
==
is this legal?
int i;
for(i=4, int t=6; i<0; i++)
===
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could actually try compiling this code yourself and see if you get any errors

Rob
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's legal. Don't see the trap! Why won't it be legal?
It just won't do a thing!!!
[ February 07, 2002: Message edited by: Younes Essouabni ]
 
YekSoon Lok
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the answer posted by JavaRanch is:
===
No.
You can declare or initialize more than one variable in a for loop, but you can't mix declaration with initialiazation.
====

any idea what it means?
 
Marshal
Posts: 15568
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the code
int i;
for(i=4, int t=6; i<0; i++)
i=4 is an initialization while
int t=6 is a declaration/initialization
This does not conform with the valid syntax for the ForInit part mentioned in the JLS, which should be in the form of a local variable declaration. See the specifications for local variable declaration statements (JLS �14.4).
Here's one that does work though (for illustration purposes only):
 
Did you just should on me? You should read this tiny ad:
ScroogeXHTML 8.7 - RTF to HTML5 and XHTML converter
https://coderanch.com/t/730700/ScroogeXHTML-RTF-HTML-XHTML-converter
    Bookmark Topic Watch Topic
  • New Topic