• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help to understand Head First code magnets p 119 - class MultiFor  RSS feed

 
Amie Callie
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Just wondered if someone could explain and go through the iterations of the code below, to reach the output:

0 4
0 3
1 4
1 3
3 4
3 3

I am new to java to have been trying to figure it out and can't get my head round it!
Thank you





 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch,

I'll explain the first part and see if you can complete the rest.

The first (outer) for loop declares 'x' as a variable of type int and sets it to 0.
'x' is tested to see if it is less than 4 and as it is the body of the for loop executes.
The second (inner) for loop declares 'y' as a variable of type int and sets it to 4.
'y' is tested to see if it is greater than 2 and as it is the body of the for loop executes.
Print the value of 'x' and 'y' to the terminal (write down the values of X and Y on a piece of paper)
'y' is decremented in the for loop statement
'y' is tested to see if it is greater than 2 and if it is the body of the for loop executes. Once 'y' is no longer greater than 2 the for loop exits.
If 'x' equals 1 increment 'x' (first time through it is 0, the second time through it is 1, the third time through it is ...)
'x' is incremented in the for loop statement
'x' is tested to see if it is less than 4 and as it is the body of the for loop executes.
....
 
Amie Callie
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you that's very helpful! I was going wrong by not staying in the inner for loop until it was completed, but all clear now
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to hear it helped you work out where you were going wrong.
 
zicheng lu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Welcome to the Ranch,

I'll explain the first part and see if you can complete the rest.

The first (outer) for loop declares 'x' as a variable of type int and sets it to 0.
'x' is tested to see if it is less than 4 and as it is the body of the for loop executes.
The second (inner) for loop declares 'y' as a variable of type int and sets it to 4.
'y' is tested to see if it is greater than 2 and as it is the body of the for loop executes.
Print the value of 'x' and 'y' to the terminal (write down the values of X and Y on a piece of paper)
'y' is decremented in the for loop statement
'y' is tested to see if it is greater than 2 and if it is the body of the for loop executes. Once 'y' is no longer greater than 2 the for loop exits.
If 'x' equals 1 increment 'x' (first time through it is 0, the second time through it is 1, the third time through it is ...)
'x' is incremented in the for loop statement
'x' is tested to see if it is less than 4 and as it is the body of the for loop executes.
....

Hi Tony, could you please tell me why the second round of X still was 0? even though it supposes to be added to 1 as x++
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get paper and pencil and create two columns, one labelled X and the other Y. Now go through the code (or my explanation if you find that easier) and whenever either one of the X or Y values change write the new value in the next row of that column. When you get to the print statement you can look at the latest values in each column to see what will be printed.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!