# Nested Loops?

Discussion in 'OT Technology' started by MaxMouse, Dec 16, 2007.

1. ### MaxMouseNew Member

Joined:
Jun 7, 2003
Messages:
197
0
Location:
toronto
Anyone know where I can find a good tutorial on nested loops and/or loops in general? Language is not too important, but Java or C/C++ would be preferred.

2. ### ge0New Member

Joined:
Oct 31, 2005
Messages:
8,398
0
Location:
JERSEY
ugh..

post your questions here and we cna answer them.. there isn't much to nested loops

3. ### DneprGuest

ArrayList i = new ArrayList();
ArrayList b = new ArrayList()
// load some stuff into both array lists
for(Object z : i) {
for(Object x : b) {
//Do stuff
}
}

I's java code btw. Nested for loops

4. ### deusexaetheraOT Supporter

Joined:
Jan 27, 2005
Messages:
19,712
0
Nested loops are easy.

Start Loop 1
Start Loop 2
Do something
Repeat Loop 2
Repeat Loop 1

5. ### tyrionlannisterNew Member

Joined:
Jun 13, 2006
Messages:
710
0
Location:
New York
A simple nested loop to create a multiplication table in Java.

Code:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
System.out.println(i + " * " + j + " = " + i*j);
}
}
This produces:

Code:
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25

6. ### tyrionlannisterNew Member

Joined:
Jun 13, 2006
Messages:
710
0
Location:
New York
Oops, that's not a table, that's just multiplication.

This is a multiplication table:

Code:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
System.out.print( i*j + "\t");
}
System.out.println();
}
Which produces:
Code:
1    2    3    4    5
2    4    6    8    10
3    6    9    12    15
4    8    12    16    20
5    10    15    20    25

7. ### ez4me2c3dCold Member

Joined:
Sep 19, 2003
Messages:
1,836
0
Location:
Minneapolis, MN
8. ### MaxMouseNew Member

Joined:
Jun 7, 2003
Messages:
197
0
Location:
toronto
I'm trying to print this:

Code:
1
21
321
4321
54321
654321

So far I have this:

Code:
for(int i = 1; i <=6; i++)
{
int spaces = 6 - i;
for(int j = 0; j < spaces; j++)
System.out.print(" ");

for(int k = 1; k <= i; k++)
System.out.print(k);

System.out.println();
}

Which prints this:
Code:
1
12
123
1234
12345
123456

I just need to know how to reverse each line.

Last edited: Dec 17, 2007
9. ### skinjobActive Member

Joined:
Jan 6, 2001
Messages:
2,337
0
Location:
Aztlán
The loop counter doesn't always have to increase.
You can have a decreasing counter like this:

for (int i = 6; i >= 1; i--)
{
...
}

10. ### noonget high and teach me how to listen

Joined:
May 4, 2002
Messages:
3,384
0
Location:
Lawrence, KS
here you go

Code:
for(int i = 1; i <=6; i++)
{
int spaces = 6 - i;
for(int j = 0; j < spaces; j++)
System.out.print(" ");

for(int k = i; k >=1 ; k--)
System.out.print(k);

System.out.println();
}

11. ### MaxMouseNew Member

Joined:
Jun 7, 2003
Messages:
197
0
Location:
toronto
I got it by doing it this way.

Code:
for(int i = 1; i <=6; i++)
{
int spaces = 6 - i;
for(int j = 0; j < spaces; j++)
System.out.print(" ");

for(int k = 6; k > spaces ; k--)
System.out.print(k-spaces);

System.out.println();
}

Thanks for the help.