• Post Reply Bookmark Topic Watch Topic
  • New Topic

What´s the difference?  RSS feed

 
Renato Bello
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Solving one of the exercises in the book head first, I solved the exercise in a different way. When I compiled the two codes, the result was the same. I wonder if my code is a possible variation or is wrong in somewhere and by coincidence it worked?

Thanks

My Code:



Head First book code:

 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Renato Bello wrote:I got a different conclusion

It would help if you told us what the difference was.
 
Renato Bello
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adrian Burkett wrote:
Renato Bello wrote:I got a different conclusion

It would help if you told us what the difference was.


I meant "conclusion", referring me to a different logic in the composition of the script, in other words, how I solved the exercise. The results were similar
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. I'm a little confused now. What's your actual question ?

Edit: Now the original post has been edited, the question is clearer and has been answered by Jelle below.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Semantically the two code blocks are identical.
The only difference is that your code initializes the counter to 0 and increments it at the end of the loop, whereas the other solution initializes the counter to -1 and increments it at the beginning of the loop before it is used.
 
Renato Bello
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle Klap wrote:Semantically the two code blocks are identical.
The only difference is that your code initializes the counter to 0 and increments it at the end of the loop, whereas the other solution initializes the counter to -1 and increments it at the beginning of the loop before it is used.


So my code is a possible variation? There is nothing wrong with it?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Renato Bello wrote:There is nothing wrong with it?

The most important thing about any piece of code is that it produces the required result. If your program produces the same output as the one in the book and there were no other restrictions stated (I don't have the book, so i don't know) then there is nothing wrong with your program.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Renato Bello wrote:
Jelle Klap wrote:Semantically the two code blocks are identical.
The only difference is that your code initializes the counter to 0 and increments it at the end of the loop, whereas the other solution initializes the counter to -1 and increments it at the beginning of the loop before it is used.


So my code is a possible variation? There is nothing wrong with it?


Yes, it's a possible variation - one of many, and no there's nothing wrong with it. I acutally prefer your approach.
 
Renato Bello
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle Klap wrote:I acutally prefer your approach.

Any reason why ? Personally I prefer the book approach. Initialising a variable (z = -1 on line 5) and then immediately changing it (on line 7) without making use of the initial value seems a little wrong to me.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adrian Burkett wrote:
Jelle Klap wrote:I acutally prefer your approach.

Any reason why ? Personally I prefer the book approach. Initialising a variable (z = -1 on line 5) and then immediately changing it (on line 7) without making use of the initial value seems a little wrong to me.


That is the book's approach ;)
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle Klap wrote:That is the book's approach ;)

Oops. Got the code snippets mixed up there. Sorry
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!