• Post Reply Bookmark Topic Watch Topic
  • New Topic

For loop in a for loop confusion  RSS feed

 
Ranch Hand
Posts: 49
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey programming peeps - I am reading "Head First Java" and am on page 121 - one of the activities has you put in a line of code into a larger program and figure out what the output is - I cannot figure out how they got the outputs that they did - here is one of the input/output answers:



The input line of code that I put in is the "x = x+3;". They say that the output of that will be "54 6" and I get nowhere near that.

THANKS for the help!
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Franklin Marquette wrote:They say that the output of that will be "54 6" and I get nowhere near that.

So what do you get ? I just ran that code and I get the expected output.
 
Franklin Marquette
Ranch Hand
Posts: 49
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well yes if I actually run and compile the code I get that - but this was an exercise where I was supposed to figure out the output in my head - so I was wondering why that is the output - I get "18 22" which is not even close to correct.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't try it in your head. Write it down and you get something like this:-Obviously that is for the other exercise on page 8375873487536
 
Franklin Marquette
Ranch Hand
Posts: 49
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay cool - I get it now - I just wasn't running the inner for loop again every time the outer for loop incremented - THANKS for the help!
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!