• 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

help needed with variables during a while loop

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

Could someone please explain why the following code produces the output 00 11 23 36 410:


I understand why the x value is 0 1 2 3 4, but why is the y value 0 1 3 6 10
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Will Dev wrote:I understand why the x value is 0 1 2 3 4, but why is the y value 0 1 3 6 10


 
Will Dev
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your response Liutauras.

So the y value maintains it's value from the previous loop each time. is that a mathematical thing or a java while loop thing.
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Will Dev wrote:So the y value maintains it's value from the previous loop each time.

Well, yes. y value being stored in a memory, every time loop iterates it uses from previous iteration stored y value.

=Will Dev wrote:is that a mathematical thing or a java while loop thing

Well, both. Probably more how the language is built. Difficult question...

By the way, welcome to the Ranch
 
Greenhorn
Posts: 19
3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Will Dev wrote:Thank you for your response Liutauras.

So the y value maintains it's value from the previous loop each time. is that a mathematical thing or a java while loop thing.



I'd say it's a scope of the variable thing.

When a variable is declared, it has a scope - a section of the code it is valid within. When you have a for loop:



i is only valid for the scope of the for loop - the section between the two curly braces. It's called a block variable.

In this code, x and y are local variables - they are only valid within the main() method. If you called another method, x and y would not be valid in that method because they only exist in main() (although, a method you call could have its *own* variables called x and y, they'd just be different ones from *these* x and y.)

What do you think would happen if you moved y so it was declared within the while loop?

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

Dor Burd wrote:

What do you think would happen if you moved y so it was declared within the while loop?



I was thinking it would be the same because although the y variable is declared outside the while loop, it is initialized inside the while loop, but because the y variable is declared outside the while loop that is the container stored in memory and that's why it holds the value after each loop...?
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Will Dev wrote:I was thinking it would be the same because although the y variable is declared outside the while loop, it is initialized inside the while loop, but because the y variable is declared outside the while loop that is the container stored in memory and that's why it holds the value after each loop...?


Line 4 initializes y. Line 6 reassigns a value to y based on y's current value (this is not initialization).
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would be easier to see the structure of the code if your indentation was not so odd.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code clean up is important.
 
Will Dev
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much everyone for the assistance, i have a good understanding now of dealing with variables during while loops and with while loops it's self.
 
reply
    Bookmark Topic Watch Topic
  • New Topic