• Post Reply Bookmark Topic Watch Topic
  • New Topic

MixFor5 confusion  RSS feed

 
Julian Cadavid
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I've been trying to figure out this exercise on HeadFirst Java page 121
I tried following what others did to figure it out but I think I'm just over thinking it.
So from what I understand
Outer starts at 0: if outer is < 3 add +1 to current value so for example
0 + 1
1 + 1
2 + 1
3, reset?
Inner 4:
4 - 1
3 - 1
2 - 1

 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Have you tried writing down the values of the different fields as you go through that program. It may take some time. You can also look at this old thread, which I think is about a similar question.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Outer starts at 0, and is 0 throughout the first loop; it doesn't become 1 until the very end of the loop.
Inner starts at 4 and remains 4 until the end of the loop. You repeat the inner loop (until x becomes 6, when the inner loop is broken).

When you repeat the outer loop, the inner loop starts again from its beginning.
 
Julian Cadavid
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Outer starts at 0, and is 0 throughout the first loop; it doesn't become 1 until the very end of the loop.
Inner starts at 4 and remains 4 until the end of the loop. You repeat the inner loop (until x becomes 6, when the inner loop is broken).

When you repeat the outer loop, the inner loop starts again from its beginning.

Hi, thanks for the welcome
I'm not understanding the for loop too much I guess.
I thought the loop was supposed to run until the value was no longer true
How come outer stays at 0 for 3 loops?
for example


Output

0
1
2
3
4

How come this one is

Output

0
0
0
1
1
1
2
2
2
 
Tim Harris
Ranch Hand
Posts: 57
3
Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Julian Cadavid wrote:
I'm not understanding the for loop too much I guess.
I thought the loop was supposed to run until the value was no longer true


You have this part correct. Look at the following for loop:


An integer called counter is being created, starting at zero. The system prints out the number of the counter, and then increments the counter. It does this until the loop is no longer true. So this will print:
0
1
2

What I think is confusing you is loops within loops.
Look at the following:



When entering the outer loop, the program then prints the first line of the outer loop. Then it begins to follow through the inner loop, until the inner loop is completed. After the inner loop is completed, the outer loop adds an extra space, goes back around and begins again. If you executed this code, it would look like this:

This statement is in the outer loop: 0
This statement is in the inner loop: 0
This statement is in the inner loop: 1
This statement is in the inner loop: 2

This statement is in the outer loop: 1
This statement is in the inner loop: 0
This statement is in the inner loop: 1
This statement is in the inner loop: 2

This statement is in the outer loop: 2
This statement is in the inner loop: 0
This statement is in the inner loop: 1
This statement is in the inner loop: 2

So, each run of the inner loop is completed before the outer loop begins its next iteration.

I hope this helps!
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have got it there. Spot on. You get however many iterations of the inner loop before the outer loop is repeated. I think you get the outer number 4, 3, 2 but not 1.

Tim Harris welcome to the Ranch
 
Julian Cadavid
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Harris wrote:
Julian Cadavid wrote:
I'm not understanding the for loop too much I guess.
I thought the loop was supposed to run until the value was no longer true


You have this part correct. Look at the following for loop:


An integer called counter is being created, starting at zero. The system prints out the number of the counter, and then increments the counter. It does this until the loop is no longer true. So this will print:
0
1
2

What I think is confusing you is loops within loops.
Look at the following:



When entering the outer loop, the program then prints the first line of the outer loop. Then it begins to follow through the inner loop, until the inner loop is completed. After the inner loop is completed, the outer loop adds an extra space, goes back around and begins again. If you executed this code, it would look like this:

This statement is in the outer loop: 0
This statement is in the inner loop: 0
This statement is in the inner loop: 1
This statement is in the inner loop: 2

This statement is in the outer loop: 1
This statement is in the inner loop: 0
This statement is in the inner loop: 1
This statement is in the inner loop: 2

This statement is in the outer loop: 2
This statement is in the inner loop: 0
This statement is in the inner loop: 1
This statement is in the inner loop: 2

So, each run of the inner loop is completed before the outer loop begins its next iteration.

I hope this helps!

Thank you SO much dude, I completely understand now! WOW Beautifully explained, thanks!
Now, i'm trying to figure out
why does x go from 0 to 6 then 6 to 12, and then 12 to 18, isn't is adding 6 everytime?
Also with the y, decrements 2, then 4?

 
Tim Harris
Ranch Hand
Posts: 57
3
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Julian Cadavid wrote:
why does x go from 0 to 6 then 6 to 12, and then 12 to 18, isn't is adding 6 everytime?

It is adding six, technically. What it's really doing is assigning x the value of x + 3; but it's doing so in two different places.
Look at lines 10 and 17 of the code and you'll see:

Both of those pieces are happening within the inner loop.

Julian Cadavid wrote:
Also with the y, decrements 2, then 4?


Similar to the x statement, the y statement appears twice as well - however, the second occurrence of it happens in the outer loop, so a total of 4 is taken from y on every outer loop.
Check lines 13 and 19 for those.
 
Julian Cadavid
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Harris wrote:
Julian Cadavid wrote:
why does x go from 0 to 6 then 6 to 12, and then 12 to 18, isn't is adding 6 everytime?

It is adding six, technically. What it's really doing is assigning x the value of x + 3; but it's doing so in two different places.
Look at lines 10 and 17 of the code and you'll see:

Both of those pieces are happening within the inner loop.

Julian Cadavid wrote:
Also with the y, decrements 2, then 4?


Similar to the x statement, the y statement appears twice as well - however, the second occurrence of it happens in the outer loop, so a total of 4 is taken from y on every outer loop.
Check lines 13 and 19 for those.

Wow, I got confused, got it thanks alot man
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!