• 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

Difference between two Java codes

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone !!!
Can anybody explain me the difference between these two codes ?


output of code no 1
small x
small x


output of code no 2
small x

Why does output differ when x is decremented before or after x ?
Is this while and do while situation ?

Thanks
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your code 1 you are decrementing x twice and in the code 2 you are decrementing it once. Also are you sure the output you have given is correct?
 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:In your code 1 you are decrementing x twice and in the code 2 you are decrementing it once. Also are you sure the output you have given is correct?


Dear Ankit Garg
There were some mistakes in the code.
I corrected them.
Can you see it now please ?
 
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you have eclipse or netBeans with you. Then the best thing is press "step into" button to debug(Normally F7 key). By pressing that key you can see how variables are declared values.


For both cases, while loop is run for 4 times.

1st Code:
x==5: x is decreased by one. Now x=4. Therefore if is not running.
x==4: x is decreased by one. Now x=3. Therefore if is not running.
x==3: x is decreased by one. Now x=2. Therefore if is running. print "small x"
x==2: x is decreased by one. Now x=1. Therefore if is running. print "small x"
x==1: Exit while loop


2nd Code:
x==5: If is not running as x=5. x is decreased by one. Now x=4.
x==4: If is not running as x=4. x is decreased by one. Now x=3.
x==3: If is not running as x=3. x is decreased by one. Now x=2.
x==2: If is running. print "small x". x is decreased by one. Now x=1.
x==1: Exit while loop
 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic