Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

For loop in a for loop confusion

 
Ranch Hand
Posts: 49
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79630
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79630
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done
 
reply
    Bookmark Topic Watch Topic
  • New Topic