• 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

Why doesn't the variable get updated in loop?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Considering following simple loop


I just expected j gets incremented and decremented from 0 to 16 and vise versa, but it's always 0! what am I doing wrong here?
 
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My, that is hard code to read!
But when I had sorted out the formatting by putting spaces round all your operators, I realised that the whole things boils down to
j = j++;
That never works.
If you search this forum you will find that lots of people get that sort of confusion, and we even have an FAQ about it.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have fallen into the j = j++ trap. Recall what the postfix increment operator does.

Rezniv Roscky wrote:Considering following simple loop


This is not a simple loop. You really should remove the k and j variables, because it's not immediately clear that the only actual iteration counter is just i.
 
Ranch Hand
Posts: 165
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow thats an intense loop!
As others already said, problem is with the j++ but just to add that if you do j+1 instead it should do what you expected. (and j-1 correspondingly).
But the code needs restructuring as already stated.
 
reply
    Bookmark Topic Watch Topic
  • New Topic