# Thread: Problem with nested for loops?

1. Member
Join Date
Jan 2014
Posts
5
Rep Power
0

## Problem with nested for loops?

Hi , Could you please help me figure out how to solve this problem?

Modify your code from the previous problem to produce the following output:

99999888887777766666555554444433333222221111100000
99999888887777766666555554444433333222221111100000
99999888887777766666555554444433333222221111100000
99999888887777766666555554444433333222221111100000
99999888887777766666555554444433333222221111100000

This is what i have but its wrong idk why ?

Java Code:
for(int*i*=*0;i*<=*9;i++)
for*(int*j*=*0;j*<=5;j++)
System.out.println(i);
System.out.println("");

2. Senior Member
Join Date
Jan 2013
Posts
168
Rep Power
5

## Re: Problem with nested for loops?

Well for starters if you want to nest a for loop you will need brackets. i.e:

Java Code:
for(int i = 0; i < 10; i++) {
for(int j = 0; j < 10; j++) {
// code here
}
}

3. ▼ dafuq did I do?
Join Date
Dec 2013
Posts
204
Rep Power
0

## Re: Problem with nested for loops?

Right along after, if you want to print from high to low, start at 9 and go down to 0 in the first for loop, in the next for loop (the nested one) go from 0-5 and print the value from the first for loop.

4. Senior Member
Join Date
Jan 2013
Posts
168
Rep Power
5

## Re: Problem with nested for loops?

So here's what you should do.

Java Code:
for(int i = 9; i >= 0; i--) {
for(int j = 0; j < 5; j++) {
System.out.print(i);
}
}

5. Member
Join Date
Jan 2014
Posts
5
Rep Power
0

## Re: Problem with nested for loops?

so i could i do something like this

Java Code:
for(int i = 9; i >= 0; i--) {
for(int j = 0; j < 5; j++) {
for(int k = 0; k <= 5; k++){
System.out.print(i);
if(j==5 && k==5)
System.out.println("");
}
}
}

6. Senior Member
Join Date
Jan 2013
Posts
168
Rep Power
5

## Re: Problem with nested for loops?

Why do you need 3 for loops? You do not need to k for loop because it will make you print out i 25 times.

EDIT: What you need is the k loop to surround both the i and j for loops. And then after the j for loop you can just print out a new line to make your output on the next line.
Last edited by pj6444; 01-23-2014 at 03:01 AM. Reason: More Details

7. ▼ dafuq did I do?
Join Date
Dec 2013
Posts
204
Rep Power
0

## Re: Problem with nested for loops?

Yes, pj's solution will work

8. Member
Join Date
Jan 2014
Posts
5
Rep Power
0

## Re: Problem with nested for loops?

i dont uinderstand i am sorry .

9. Señor Member
Join Date
Jan 2014
Posts
184
Rep Power
0

## Re: Problem with nested for loops?

As far as I can tell, PJs solution won't work - try running it, and you'll get one long line.

You need to add this.

Java Code:
for(int i = 9; i >= 0; i--) {
for(int j = 0; j < 5; j++) {
System.out.print(i);
}
System.out.println("");
}
To explain to you what is happening:

With just one forloop, you are printing out something everytime the forloop repeats itself.

Aka, if you have
Java Code:
for(int i = 0; i < 5; i++) {
System.out.print("-");
}
You'll get
-----
as an output.

To make it so that you get multiple lines, you nest that forloop inside of another forloop, that'll print out that line with every iteration.

You must remember to add a println in every iteration of the 'line' forloop - basically like pressing "enter" when you're typing a word document

10. ▼ dafuq did I do?
Join Date
Dec 2013
Posts
204
Rep Power
0

## Re: Problem with nested for loops?

Originally Posted by AlexGraal
As far as I can tell, PJs solution won't work - try running it, and you'll get one long line.

You need to add this.

Java Code:
for(int i = 9; i >= 0; i--) {
for(int j = 0; j < 5; j++) {
System.out.print(i);
}
System.out.println("");
}
To explain to you what is happening:

With just one forloop, you are printing out something everytime the forloop repeats itself.

Aka, if you have
Java Code:
for(int i = 0; i < 5; i++) {
System.out.print("-");
}
You'll get
-----
as an output.

To make it so that you get multiple lines, you nest that forloop inside of another forloop, that'll print out that line with every iteration.

You must remember to add a println in every iteration of the 'line' forloop - basically like pressing "enter" when you're typing a word document
I was referring to his edit, where he makes a remark about adding the 3rd for loop and changing the println position, like so:

Java Code:
for (int i = 0; i < 5; i++) {
for (int j = 9; j >= 0; j--) {
for (int k = 0; k < 5; k++) {
System.out.print(j);
}
}

System.out.println();
}
EDIT: Oh god the syntax highlighter needs indenting.

11. Señor Member
Join Date
Jan 2014
Posts
184
Rep Power
0

## Re: Problem with nested for loops?

Oh my bad. Sorry, I wasn't reading carefully enough I guess!

12. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,743
Rep Power
10

## Re: Problem with nested for loops?

Here you go!

Java Code:
System.out.println("99999888887777766666555554444433333222221111100000");
System.out.println("99999888887777766666555554444433333222221111100000");
System.out.println("99999888887777766666555554444433333222221111100000");
System.out.println("99999888887777766666555554444433333222221111100000");
System.out.println("99999888887777766666555554444433333222221111100000");
You no longer have a problem with nested loops.

Regards,
Jim
Last edited by jim829; 01-23-2014 at 06:55 AM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•