• Post Reply Bookmark Topic Watch Topic
  • New Topic

simple question with variable declaration and reset variable to init state  RSS feed

 
        
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
simple code



why variable is a=6 and not 10 in the last 2 lines?
Is it possible to reset variable a as it was declared a=10?

run:
test
10
9
8
7
6
1006
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because you are changing it in your loop on line 10.

and if you want to reset it to 10, just put in

a = 10;

alternatively, you could copy the value of a to some temp variable, use that in your loop, and a would remain unchanged.
 
Daryl Cofer
Greenhorn
Posts: 19
Eclipse IDE Java Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your code you have this block:

What line 5 says is:
1. Take variable a
2. Subtract 1 from it
3. Store whatever that number is into a.

So basically the reason that a does not equal 10 at the end of your program is because it is being overwritten each time by line 5 in the code block above.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!