• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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

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





 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad to hear it helped you work out where you were going wrong.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic