Forums Register Login

Chapter 2 - The for statement 2. Adding Multiple Terms to the for statement Java OCA 8,Sybex

+Pie Number of slices to send: Send
Hi

I'm using the electronic version so I don't have page numbers to refer to.

In chapter 2, under the section "The for Statement", example "2. Adding Multiple Terms to the for Statement



the output the book says I should get is:
0 1 2 3 4

the output I get is:
0 1 2 3 4 5

I've triple checked the code I'm compiling and I'm sure I haven't entered a typo, and the output I'm getting makes sense to me, here's my reasoning:

at the end of the fifth iteration x=4, y=4
so on the next iteration (sixth) the boolean expression evaluates to true, and x and y are each incremented to 5
boolean expression now evaluates to false, so there is no seventh iteration
at the end of the for loop, printed out is "0 1 2 3 4"
after the for loop you have the print statement that prints out the value of x, which is 5, so you have
"0 1 2 3 4 5"

Where is my logic going wrong? Or have I indeed typed something incorrectly.

Thankye all kindly
+Pie Number of slices to send: Send
Tom,
You typed and understood it correctly. In the print book, this is on page 82 and logged in the errata as "In the first example on the page, System.out.print(x) should be removed". That's where the stray 5 is coming from. The line that shouldn't be there.

Thanks for asking rather than staying confused!
+Pie Number of slices to send: Send
Thanks for the help Jeanne, and sorry, I did check the errata, but clearly not thoroughly. I'll be sure to go over the errata in depth before posting another question.
+Pie Number of slices to send: Send
I don't think you could have figured out it was the same thing without knowing the page number in the printed book.

I wasn't criticizing; just explaining why I didn't add it to the errata after your thread ;).
+Pie Number of slices to send: Send
Thanks Jeanne

You're uber awesome for taking the time out for your readers
+Pie Number of slices to send: Send
even the semi-daft ones like me
+Pie Number of slices to send: Send
You're welcome. And don't worry. The daft ones are quietly confused and not learning. You are smart for asking!
+Pie Number of slices to send: Send
 

Jeanne Boyarsky wrote:And don't worry. The daft ones are quietly confused and not learning. You are smart for asking!


And the very smart ones, use each code snippet to do some experimenting and twiddling to learn even more

So let's give you a hand and create a bunch of examples with some minor changes to the original code snippet. And the question each time is the same: what's the output? Will it be different than the original one? Here we go!

Example 1: use bitwise & operatorExample 2: use postfix increment operator in condition expressionExample 3: use prefix increment operator in condition expressionExample 4: use conditional || operator and prefix increment operatorExample 5: use bitwise | operator (and prefix increment operator)Example 6: use conditional || operator and postfix increment operatorExample 7: use bitwise | operator (and postfix increment operator)

So much fun with such a small code snippet!

Hope it helps!
Kind regards,
Roel
It's never done THAT before. Explain it to me tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 852 times.
Similar Threads
switch statement
Please Help
Dan's exam: Question 4, Langauage Fundamentals, Main Method
Question about results of code
Updated K&B 5 Errata - 4/16
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 05:03:55.