programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Jeanne Boyarsky
• Devaka Cooray
• Paul Clapham
Sheriffs:
• Tim Cooke
• Knute Snortum
• Bear Bibeault
Saloon Keepers:
• Ron McLeod
• Tim Moores
• Stephan van Hulst
• Piet Souris
• Ganesh Patekar
Bartenders:
• Frits Walraven
• Carey Brown
• Tim Holloway

Problems with two connected for-loops

Greenhorn
Posts: 2
Hello everyone.

I am learning Java at the moment and I have problems to understand connected for-loops.
Let me explain what I mean by some quite easy example:

The outcome is simple as that:

Ok so my thoughts are these:

- first check if 1 < 3. That's true, enter the instructions in the inner loop
- check if j <= i, meaning 1 <= 1. True, print out one Star *.
- finished instructions in the inner loop, so next step is j++, meaning j = 2.
- check if j <= i, meaning 2 <= 1. That's wrong, the inner loop is done.
- print out new line
- outer loop: i++ (must be increased). Then i = 2 = j.

- ok, now enter outer loop again (condition true)
- inner loop: j <= i, meaning 2 <=2 = true.
- =>=>=> print ONE Star *.

I don't understand, why it prints 2 Stars at that point. Any help would be highly appreciated

Ranch Hand
Posts: 56

- outer loop: i++ (must be increased). Then i = 2 = j.

Why i = 2 = j? The inner loop starts again with j = 1.

Ranch Hand
Posts: 82
2

The inner loop is working as expected. The first time i is =1, thus once through the inner loop, when i is equal to 2, the program will loop twice in the inner loop.

Bartender
Posts: 1561
As i see it, the steps are:
• i is first 1
• check that i is < 3: it is
• start inner loop: j is 1
• check that j is < = i (now 1), and it is
• print out a "*"
• increase j by 1, it' s now 2
• check that j is < = i (now 1), and it isn't since j is 2, so exit inner loop
• println
• i is increased to 2
• check that i is < 3: it is
• start inner loop: j is 1
• check that j is < = i (now 2), and it is
• print out a "*"
• increase j by 1, it' s now 2
• check that j is < = i (now 2), and now it is; so print out another (the second) "*"
• increase j by 1, it' s now 3
• check that j is < = i (which is 2), and now it isn't; so exit inner loop
• println
• i is increased to 3
• check that i is < 3: it isn't, and so exit outer loop

•
lowercase baba
Posts: 12751
51
I think you have some slight errors in your logic... You seem ok, up to here:

- outer loop: i++ (must be increased). Then i = 2 = j.

Note that since j was declared inside the inner loop, and you are now outside of the inner loop, there is no j. but yes, i == 2.

Then you RESTART your inner loop, which re-declares j and sets it equal to 1.

so, 1 is <=2, so we print a star.
we increment j, so j == 2.
2 <= 2, so we print a start (on the same line)
increment j to 3, break out of inner loop.
print new line
increment i to 3, so we break out of that loop.

Bartender
Posts: 1840
12

Mustang Bulton wrote:

Ok so my thoughts are these:

- first check if 1 < 3. That's true, enter the instructions in the inner loop
- check if j <= i, meaning 1 <= 1. True, print out one Star *.
- finished instructions in the inner loop, so next step is j++, meaning j = 2.
- check if j <= i, meaning 2 <= 1. That's wrong, the inner loop is done.
- print out new line
- outer loop: i++ (must be increased). Then i = 2 = j.

- ok, now enter outer loop again (condition true)
- inner loop: j <= i, meaning 2 <=2 = true.
- =>=>=> print ONE Star *.

You almost have it.

But when you increment for the outer loop, the inner loop resets. It's like a new round. So you get (on the second round)

i == 2 which is less than 3. Ok go ahead.
j == 1 which is less than or equal to 2. Ok go ahead... output a star, increment j.
j == 2, also less than or equal to 2. Another star. go to the next line. increment j.
j == 3, STOP increment outer loop
i == 3, STOP.

You will notice a change if you make the following change:

Mustang Bulton
Greenhorn
Posts: 2
Ahhhhhhh thanks people

I really thought more than 1 hour about that. Didn't know that the inner loop resets each time.

Best regards,
M

Marshal
Posts: 64666
225
Welcome to the Ranch

fred rosenberger
lowercase baba
Posts: 12751
51
a good way to figure out what's going on is to use System.out.println() liberally. you could have done something like this:

I indented the printing of 'j' so that is mimics the loop structure. It makes it a little easier to to follow the output.

Note that to do this, I had to put braces in for your inner loop. a common mistake people make is to write their loops without braces, then add a line of code, which then kicks the other line out of the loop body.

most folks would recommend you ALWAYS use braces, even when the loop body is only one line.

 It is sorta covered in the JavaRanch Style Guide.