• Post Reply Bookmark Topic Watch Topic
  • New Topic

For loop confusion  RSS feed

 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Ranch:

I am utterly confused about something I read in the Oracle OCA book about for loops. One of the "special cases" of for loop where a variable is redeclared in the initialization block.



The code above builds successfully even though I have not given x a data type. To add to my confusion, if I attempt to remove the comment from int x = 0; then I incur the compiler's wrath. When I remove the comment from long y = 10; same result, except that part makes sense.

I originally tested this code snippet from my book thinking it might be a typo. Sure enough, it doesn't compile. But it will if I remove long from y in the initialization block and declare it above the loop, then the compiler is happy.



Can someone please straighten me out? Why does the top code compile without declaring x?
 
Omkar Shetkar
Ranch Hand
Posts: 100
2
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In for loop init, you can declare multiple variables. In this case, both x and y are of type long.
In Java, you can't declare same variable name multiple times as either class field or local variable.
This explains why compiler complains when you declare variable x before for loop.

 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But this will also not work:



They are multiple variables of the same type.
 
Omkar Shetkar
Ranch Hand
Posts: 100
2
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Init statement of for loop should a valid Java statement. By valid statement I mean it should compile even outside of for loop.

is not a valid Java statement.

You get error as "Syntax error on token ",", ; expected"

By the way, title of this thread should be corrected to "For loop confusion"
 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It makes sense now. Thanks!
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Omkar Shetkar wrote: . . . you can't declare same variable name multiple times as either class field or local variable. . . .
That bit sounds correct, but is difficult to understand.
You can only have one field with a particular name. You can only have one local variable/parameter with a particular name in scope at any one time.
But you can have a local variable and a field with the same name and the local variable shadows the field.
 
Omkar Shetkar
Ranch Hand
Posts: 100
2
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:But you can have a local variable and a field with the same name and the local variable shadows the field.

Completely agreed Campbell.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Travis Roberts wrote:But this will also not work:They are multiple variables of the same type.

In which case you must do it as you wrote before, ie:
  for(long y = 0, x = 4; x < 5 && y < 10; x++, y++) { ...
and in fact you're far better off to use the correct literal type, viz:
  for(long y = 0L, x = 4L; x < 5L && y < 10L; x++, y++) { ...

What I'm not sure about is whether
  long x = 0L;
  for(long y = 0L, x = 4L; x < 5L && y < 10L; x++, y++) { ...
will compile or not (I suspect not).

Try it and see.

Winston
 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Travis Roberts wrote:But this will also not work:They are multiple variables of the same type.

In which case you must do it as you wrote before, ie:
  for(long y = 0, x = 4; x < 5 && y < 10; x++, y++) { ...
and in fact you're far better off to use the correct literal type, viz:
  for(long y = 0L, x = 4L; x < 5L && y < 10L; x++, y++) { ...

What I'm not sure about is whether
  long x = 0L;
  for(long y = 0L, x = 4L; x < 5L && y < 10L; x++, y++) { ...
will compile or not (I suspect not).

Try it and see.

Winston


That bottom bit of code does not compile.

I was forgetting that the x defined outside of the for loop is considered inside of it and in the parentheses. Easy enough but I had been cramming so much into my head it just didn't compute at the time.
 
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!