• Post Reply Bookmark Topic Watch Topic
  • New Topic
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problems with two connected for-loops

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

- 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 13089
    67
    Chrome Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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: 1849
    15
    Eclipse IDE Spring VI Editor Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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: 79178
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Welcome to the Ranch
     
    fred rosenberger
    lowercase baba
    Posts: 13089
    67
    Chrome Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic