# nested for loop

• April 17th, 2013, 01:21 AM
tardis_
nested for loop
good morning, i've been stuck on this issue for about 2 weeks now! :-(
i have a nested loop.
Code :

```for(i= 0; i<= 3; i++) { for(j= 0; j<= 8; j++) { System.out.println(i+ " " + j); } }```

i know this has an output 01 0,2 0,3, 04 0,5 0,6 0,7 0,8 1,0......1,1 1,2 1,3 1,4 1,5 1,6 1,7 1,8 2,0...ETC

what i want to do is if i set the second for loop variable j to take an inputted integer from the user (say 4) how do i reset it to 0 once it hits 8.

the problem i keep having is that it goes back and continues the loop from 4 when i want it to reset to 0.

Thanks! :D
• April 17th, 2013, 01:48 AM
jps
Re: nested for loop
I don't see anywhere in your code where you attempt to reset your variable. Also I do not fully understand what you are trying to make the code do, please try to explain again
• April 18th, 2013, 11:16 AM
tardis_
Re: nested for loop
Quote:

Originally Posted by jps
I don't see anywhere in your code where you attempt to reset your variable. Also I do not fully understand what you are trying to make the code do, please try to explain again

it was 1am when i wrote that lol, let me try this again.
i want two loops as you see in the code to do exactly what it's doing.
00
01
02
03
10
11
12
13
20
21
22
23
etc.

but when i put in variable at j say 4, then the whole loop will start at 4 not 0. when i try to reset the variable, say inside the last loop at j= 0, i the whole loop just doesn't start, no output. What i want the code to do is that it resets at 0 regardless at where i start. so if i put say 2, then it will go from 2 to 8, then back to 0 while adding 1 to i.
• April 18th, 2013, 11:39 AM
Norm
Re: nested for loop
Quote:

i want two loops as you see in the code to do exactly what it's doing.
If so, what problems are you having with the program?

Quote:

but when i put in variable at j say 4, then the whole loop will start at 4
If you really want some different output, can you post what you want the output to look like?
• April 18th, 2013, 01:11 PM
jps
Re: nested for loop
Quote:

Originally Posted by tardis_
i want two loops as you see in the code to do exactly what it's doing.

:-?

Quote:

Originally Posted by tardis_
but when i put in variable at j say 4, then the whole loop will start at 4 not 0.
when i try to reset the variable,

How do you change j to 4? How do you reset the variable? Variables i and j are both locally declared, and the control for both loops are hard coded. If you want to vary the number of times a loop runs you will have to use a variable in place of the hard coded numerical values.
Code java:

```for( int i = iStartValue; i < iStopValue; i++){ for( int j = jStartValue; jStopValue < 8; j++) { //tasks } }```
I included variables in your code in place of hard coded values. Using variables in these 4 locations gives you the control of the loops you seem to be wanting. Modify the values of the variables outside the loop before the loop(s) start, and inside the loop(s) as the code executes, to suit your needs.