• Post Reply Bookmark Topic Watch Topic
  • New Topic

Brain Friendly Explanation to Multiple For Loops  RSS feed

 
Daniel Hirning
Ranch Hand
Posts: 50
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

First post..hopefully in the right spot.

Reading Head First Java, and a little over a hundred pages in i came across this code in a puzzle.
I was hoping someone can give a brain friendly explanation for how this multiple for loop works(does it have a name?).
I have posted my views on what i think is happening under the code below, is this correct?

Thanks,
Daniel



*Inner loop runs until y = 2;
*Outer loop runs inner until y = 2;
*If statement comes into play and the Outer keeps running the inner in this cycle until x = 4;
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you get a pencil paper and eraser (I have just bought a new eraser; this is probably the most important piece of hardware of all ‍).
Write down the starting values of the variables:
Start with x 0 y 4.
Write the next iteration of each of the loops: remember the inner loop runs several times for every time the outer loo runs
x o y 3
Keep doing that, until you get to the end of the program. That should explain everything for you
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly do you mean by “until y = 2”? You need to be very precise about that sort of thing. Remember the computer will be very pedantic about what you have written, so you need to be pedantic about how you describe it.
 
Daniel Hirning
Ranch Hand
Posts: 50
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,
Thanks for your response, I can see myself going through a lot of paper!
This is a break down of what i believe is occurring, or at least this matches the java output.
If this is the correct sequence of events, its a lot less mystical than i took it for yesterday.
Can i trouble you to look at my workings below or give that new eraser a whirl?
I just want to make sure i got this before i move on in the book.
Thanks,
Daniel

x,y;

Start Outer Loop(1st Loop) 0,4
Start Inner Loop(1st Loop) 0,4 (This value gets printed)
Start Inner Loop(2nd Loop) 0,3 (This value gets printed)
Start Inner Loop(3rd Loop) 0,2 as y is no longer greater than two break loop.
Start Outer Loop(2nd Loop) 1,4
Start Inner Loop(1st Loop) 1,4 (This value gets printed)
Start Inner Loop(2nd Loop) 1,3 (This value gets printed)
Start Inner Loop(3rd Loop) 1,2 as y is no longer greater than two break loop.
If Statement Boolean test becomes true
Start Outer Loop(3rd Loop) 3,4
Start Inner Loop(1st Loop) 3,4 (This value gets printed)
Start Inner Loop(2nd Loop) 3,3 (This value gets printed)
Start Inner Loop(3rd Loop) 3,2 as y is no longer greater than two break loop.
Start Outer Loop(4th Loop) 4,4 as x is no longer less than four break loop.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Hirning wrote:Start Outer Loop(1st Loop) 0,4

Well, that looks wrong for a start. The outer loop only involves x. Indeed if you were to put
System.out.println(y);
as the first statement inside that loop, you'd get a compiler error.

I think:
Start Outer Loop(1st Loop): x=0
would be much better.

Winston
 
Daniel Hirning
Ranch Hand
Posts: 50
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Winston,
I did know this, and in hindsight without your correction if someone else were to look at this it would be confusing so thanks.

Bearing in mind I am just tracking the values for purposes of working out the mechanics of this loop de loop, does it seem incorrect anywhere else?

Thanks in advance,
Daniel
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Hirning wrote:Bearing in mind I am just tracking the values for purposes of working out the mechanics of this loop de loop, does it seem incorrect anywhere else?

Seems fine to me; and I suspect that it's explained why you're getting the results you are, which is the main point of the exercise. The fact is that it was probably fine as it was. I just wanted to make sure you understood that y doesn't exist for the outer loop.

Winston
 
Daniel Hirning
Ranch Hand
Posts: 50
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No Worries, and thanks for your help. I just tested one that required more iterations and managed to get the result with pen and paper before compiling to check so i believe i got this down.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!