Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

help needed with variables during a while loop  RSS feed

 
Will Dev
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • 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
 
Liutauras Vilda
Marshal
Posts: 4638
316
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 4638
316
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
Dor Burd
Greenhorn
Posts: 19
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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...?
 
Carey Brown
Bartender
Posts: 2993
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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).
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 2993
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code clean up is important.
 
Will Dev
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!