• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

for loop code

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I came across something a bit strange in the OCA book (1zo-808 p 82). It says:
3)Redeclaring a variable in the initialization block

Apparently it doesn't compile because
x is repeated in the initialization block after already being declared before the loop resulting in the compiler stopping because of a duplicate variable declaration
, which is fair enough. Then the book says that if we change the code to the below, it instead compiles:

Now, this apparently works...but it seems to me that it has the same problem: in the first snippet of code, the issue was apparently x being declared outside the loop and then redeclared (although I'd object to that because x was simply assigned another value inside the loop rather than being redeclared) and in the second example none of the variable are redeclared, they're simply assigned another value, so it's effectively the same as the first...not really sure what's going on then...
 
Liutauras Vilda
Bartender
Pie
Posts: 2773
111
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jason, it seems I don't need to welcome you again

You answered actually your question yourself, but somehow in the middle of self explanation got confused again. Lets try together.

Quick note: in both scenarios code wouldn't compile because of the missing semicolons after each System.out.print statements (but probably you didn't copy those well). Lets forget it now.

1. So, why this code wouldn't compile? As you rightly mentioned yourself, because x getting declared twice. Before for loop, and inside for loop. Now why x inside the for loop doesn't have data type declared? It has, it is long. It is just the way it is, inside the for loop you can declare multiple variables by specifying data type only once and the other consequent variables separating by comma. Does that make sense?

2. This scenario is also brutally simple, as you rightly mentioned yourself, x and y variables are declared before for loop, and inside for loop they are just getting reassigned their values. As you know yourself, reassign values is legal operation, while declare same variable twice is illegal.

Basically I just repeated once again what you just said, think about it for a moment again, that should make sense, doesn't it?
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah...wait. That was very subtle...This long y = 0,x = 4; is declaring two long variables, and since we're declaring already x as an int outside the block, we're now redeclaring it inside the loop as a long...OK completely missed that, sorry! Clear now!
 
Liutauras Vilda
Bartender
Pie
Posts: 2773
111
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need to sorry. I'm glad you understood
 
Liutauras Vilda
Bartender
Pie
Posts: 2773
111
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:This long y = 0,x = 4; is declaring two long variables, and since we're declaring already x as an int outside the block, we're now redeclaring it inside the loop as a long
Exactly 
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, and yes the missing semicolons are just a typo, I realized it when I run the code! Thanks for your input!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic