It's not a secret anymore!
The moose likes Beginning Java and the fly likes for loop question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "for loop question" Watch "for loop question" New topic

for loop question

David McHugh

Joined: Oct 01, 2008
Posts: 23

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

Joined: Aug 08, 2007
Posts: 103
Can you embed the peice of code that create the problem. Then it would look fine for others to understand the problem.

Thanks<br /> <br />Anoobkumar<br />SCJP 1.5
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11879

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 ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
David McHugh

Joined: Oct 01, 2008
Posts: 23
cheers fred, just what i needed..
I agree. Here's the link:
subject: for loop question
It's not a secret anymore!