# Problem with nested for loops?

Printable View

• 01-23-2014, 02:03 AM
Jar
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 ?

Code:

```for(int*i*=*0;i*<=*9;i++) for*(int*j*=*0;j*<=5;j++) System.out.println(i); System.out.println("");```
• 01-23-2014, 02:19 AM
pj6444
Re: Problem with nested for loops?
Well for starters if you want to nest a for loop you will need brackets. i.e:

Code:

```for(int i = 0; i < 10; i++) {     for(int j = 0; j < 10; j++) {         // code here     } }```
• 01-23-2014, 02:27 AM
zFollette
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.
• 01-23-2014, 02:41 AM
pj6444
Re: Problem with nested for loops?
So here's what you should do.

Code:

```for(int i = 9; i >= 0; i--) {     for(int j = 0; j < 5; j++) {         System.out.print(i);     } }```
• 01-23-2014, 02:48 AM
Jar
Re: Problem with nested for loops?
so i could i do something like this

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("");         }     } }```
• 01-23-2014, 02:52 AM
pj6444
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.
• 01-23-2014, 03:09 AM
zFollette
Re: Problem with nested for loops?
Yes, pj's solution will work
• 01-23-2014, 03:17 AM
Jar
Re: Problem with nested for loops?
i dont uinderstand i am sorry .
• 01-23-2014, 03:29 AM
AlexGraal
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.

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
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
• 01-23-2014, 04:34 AM
zFollette
Re: Problem with nested for loops?
Quote:

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.

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
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:

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.
• 01-23-2014, 05:00 AM
AlexGraal
Re: Problem with nested for loops?
Oh my bad. Sorry, I wasn't reading carefully enough I guess!
• 01-23-2014, 06:49 AM
jim829
Re: Problem with nested for loops?
Here you go!

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