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
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Help understanding an output

Greenhorn
Posts: 17
• Number of slices to send:
Optional 'thank-you' note:
I'm doing an excercise for school and I'm not understanding where the output comes from.

It starts with this:

Here the the output of z = 81 and is crystal clear to me.

But then they change the z = x * y line into z++ like so:

And the output of z = 100 now. And that I don't get. As I'd say that it should be 1 I'm not getting something. Can someone help me understand how the z turns into 100?

Sheriff
Posts: 3063
12
• Number of slices to send:
Optional 'thank-you' note:
z++ is shorthand for z = z + 1. After doing that 100 times, you get 100.

Timothy Oldbean
Greenhorn
Posts: 17
• Number of slices to send:
Optional 'thank-you' note:

Greg Charles wrote:z++ is shorthand for z = z + 1. After doing that 100 times, you get 100.

The bit that z increments with one isn't wat confuses me. Somehow I don't get the 100 pass throughs. How does the loop pass z 100 times?

Ranch Hand
Posts: 479
• Number of slices to send:
Optional 'thank-you' note:
x loops 10 times; for every time that x loops, y loops 10 times
so for x == 0, y goes from 0 to 9
x == 1, y goes from 0 to 9

etc.

therefore, the z++ executes 100 times.

rc

Timothy Oldbean
Greenhorn
Posts: 17
• Number of slices to send:
Optional 'thank-you' note:

Ralph Cook wrote:x loops 10 times; for every time that x loops, y loops 10 times
so for x == 0, y goes from 0 to 9
x == 1, y goes from 0 to 9

etc.

therefore, the z++ executes 100 times.

rc

Aha. So I think I didn't get the first one then although I ended at the same number.

Am I right when I say that if by taking your explanation and applying that to the first one y already hits the 9 after the first go through from x and then remains that until x also reaches 9? After that it moves on to the z = x * y for the 81 output. Sure hope it does because then I get it now

I was (probably) mistankingly thinking that the loop stuck in the x until it hit 9 and only then moved on to the 2nd one until y hit 9 and then hit the z = x * y for the 81 output.

Sorry to have to ask again, guess I'm not the sharpest tool in the shed. But I figure that being afraid to ask on until you completely get it is even less smart.

Ralph Cook
Ranch Hand
Posts: 479
• Number of slices to send:
Optional 'thank-you' note:
Almost. But y doesn't get 'stuck' at 9 -- y goes from 0 to 9 while x is 0, then goes from 0 to 9 while x is 1, then again while x is 2, etc. At the end it goes from 0 to 9 while x is 9, and THEN they are both 9. Note that, in this version, z is calculated every time through the loop, with every successive product replacing the value of z that was calculated the last time.

You might benefit from having the code print out the values of x, y, and z each time through the "inner" loop, just to see this in action:

ALL of us lacked this knowledge at some point and had to learn it, just as you are doing. It is true that some of us (them?) get it or got it quicker than others, or see what it implies faster, and no one can tell from your postings how quickly you pick things up because none of us know how long you've been at it or whether you've seen any of this before.

I do think computer programming suffers from an overall attitude of many of its denizens that, if you don't know something that they know, you are somehow less than they are -- less smart, less cool, less worthwhile, etc. It is no way to treat anyone trying to learn a complex subject, and it says a lot more about the attitude holder than the attitude target.

So don't buy into this attitude, even for yourself. Yes, these particular things are basic. Learn them and move on. And remember all this later, when you're answering questions on JavaRanch.

rc

Timothy Oldbean
Greenhorn
Posts: 17
• Number of slices to send:
Optional 'thank-you' note:
Thanks a lot Ralph!

Both your explanation as running the code with the line you added with the x, y and z outputs have greatly helped me get this.