# Thread: Exercise problem, how was this output achieved?

1. Member
Join Date
Oct 2009
Posts
10
Rep Power
0

## Exercise problem, how was this output achieved?

Hi guys,

I am reading through a book on Java where they have asked us to match the correct output from a list code inputs. The book is 'Head first Java' and after writing the code I see the answers they have given are correct but I cannot work out how the code resulted in those answers and was wondering if anyone could give me a few hints...

Thanks for you time!

Bs3ac

2. Well, We might be able too.

One Problem.

Where is your code?

Please Post it!

Otherwise

Your SOL! :)

3. Moderator
Join Date
Apr 2009
Posts
13,126
Rep Power
23
The easiest way on something like this is to step through the code by hand, using pencil and paper to keep track of x and y...just make sure you loop the correct number of times (and break at the correct point of the inner loop).

4. Member
Join Date
Oct 2009
Posts
10
Rep Power
0
Hi guys,

Tolls I completley agree with you and that is how I have come about to try and understand how this code works but the bit that gets me is the two 'For' loops which uses the int inner and int outer even though there are no variables set with those values. Perhaps you could explain to me how these two for loops work in terms of the inner and outer values?

FAO:Sno, that is the whole code. That was a picture taken straight from the book. I have simply typed all that code in my java console to create the class to try and gain an understanding in how the code operates by modifying it to see its effect on the X and Y outcome...
Last edited by bs3ac; 08-05-2010 at 11:41 PM. Reason: typo

5. I have simply typed all that code in my java console to create the class
Did you try to compile it and execute it?

6. Moderator
Join Date
Apr 2009
Posts
13,126
Rep Power
23
You do know how for loops work don't you?
The first part (int outer = 0) is declaring and setting the variable...

7. Member
Join Date
Oct 2009
Posts
10
Rep Power
0
Norm: yes it compiles and runs fine and gives the expected results as those in the solutions part in the picture.

Tolls: Yes I know how loops work, the bit that was bugging me however was the 'inner' amd 'outer' part (which I think I have found the solution).

Am I right in saying that the first 'for' loop is only run twice as its '< 3' where the vairables starts at 0 but added +1 each time.

I am after the 'x' and 'y' outcome but the inner and outer for loop was bugging me as I seem to get diffrent answers than those stated... maybe I am just going through the program wrong...

8. To see how the variables change, add a println() to display their values as it loops:

System.out.println("outer=" + outer + ", inner=" + inner); // show values of variables

9. Moderator
Join Date
Apr 2009
Posts
13,126
Rep Power
23
For the outer loop:
First loop: outer = 0.
Second loop: outer = 1.
Third loop: outer = 2.
exit.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•