• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

for loop question

 
David McHugh
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

new to java so go easy!!!

i have a for loop that does some calculations then jumps back to the main program, what i want is when it jumps back to the main program to icrement an int called y by 1 and then I want it to go back into the for loop again. I have got the for loop working fine it is just the increment by 1 i am struggling with, i think because i have declared y=0 in the main program it keeps getting reset after the for loop has finished, any ideas?
 
Anoobkumar Padmanabhan
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you embed the peice of code that create the problem. Then it would look fine for others to understand the problem.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12203
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you could try another for loop


this would call the inner for loop once with a y value of 0. When the inner loop ends, you go through the outer loop again, so y would now be 1. You can adjust this to your needs.

alternatively, you could move your loop to a method, and call it twice:

this has advantages and disadvantages as well, but it is an option.
[ October 23, 2008: Message edited by: fred rosenberger ]
 
David McHugh
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cheers fred, just what i needed..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic