This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Problems with two connected for-loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problems with two connected for-loops" Watch "Problems with two connected for-loops" New topic
Author

Problems with two connected for-loops

Mustang Bulton
Greenhorn

Joined: Feb 01, 2010
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
Misha van Tol
Ranch Hand

Joined: Jan 02, 2010
Posts: 56

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

Why i = 2 = j? The inner loop starts again with j = 1.
Larry Frissell
Ranch Hand

Joined: May 16, 2008
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.
pete stein
Bartender

Joined: Feb 23, 2007
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

  • fred rosenberger
    lowercase baba
    Bartender

    Joined: Oct 02, 2003
    Posts: 11168
        
      16

    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.





    There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
    Janeice DelVecchio
    Saloon Keeper

    Joined: Sep 14, 2009
    Posts: 1659
        
      11

    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:




    When you do things right, people won't be sure you've done anything at all.
    Mustang Bulton
    Greenhorn

    Joined: Feb 01, 2010
    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
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38045
        
      22
    Welcome to the Ranch
    fred rosenberger
    lowercase baba
    Bartender

    Joined: Oct 02, 2003
    Posts: 11168
        
      16

    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.
     
    subject: Problems with two connected for-loops
     
    Similar Threads
    Loop Problem
    Explanation needed on Break and continue questions
    Stars program help
    outer inner program
    loop