• 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

simple question with variable declaration and reset variable to init state

 
        
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 19
Eclipse IDE Ruby Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic