Forums Register Login

Doubt about basic for loop without the update statement (Java OCA 8 Programmer I Study Guide, Sybex)

+Pie Number of slices to send: Send
I need help in understanding the chapter 2 question number 9 in oca8 study guide. the question is




the answer given is " code contains an infinite loop and doesnot terminate".

i could not understand why the value of "i" is reinitialized to 0 everytime the loop executes?

please clarify
+Pie Number of slices to send: Send
You might find this useful.
+Pie Number of slices to send: Send
 

Nithya Mohan anand wrote:i could not understand why the value of "i" is reinitialized to 0 everytime the loop executes?


First of all, let's rewrite the code snippet using a for loop with update statementIf you execute this code snippet, you'll notice that the result is exactly the same: the code contains an infinite loop and does not terminate. So it's definitely not related to the fact that the for loop doesn't have an update statement.

Let's change the post-increment operator to a pre-increment operator as illustrated in this code snippetWhen this code is executed, "Hello World" will be printed 10 times. So the post-increment operator is responsible for the infinite loop.

Now let's introduce another loop variable j to assign the result of the post-increment operator and see what happens. This is the adjusted code snippetWhen you execute this code snippet, "Hello World" will be printed 10 times.

The statementis equivalent toAnd that's why i is not incremented and remains 0 all the time (and that's why i will never be greater than 10 and you have an infinite loop). If you want to learn more about the post-increment operator, you can read the article mentioned by Joe. And in this topic you'll find an extensive discussion with lots of code examples. Definitely worth reading!

Hope it helps!
Kind regards,
Roel
+Pie Number of slices to send: Send
Thanks a lot Joe!!.

Roel .. thanks a lot. I got the clear explanation now. Thanks for taking time to reply..


NIthya
+Pie Number of slices to send: Send
the above link to the post that explains increment operator is great,but how do i find a topic .I am aware that i can use search to look for a topic. But what if i want to look about all the explanation given for all oca chapters,without searching for a topic.Please help
1
+Pie Number of slices to send: Send
 

Nithya Mohan anand wrote:but how do i find a topic .I am aware that i can use search to look for a topic. But what if i want to look about all the explanation given for all oca chapters,without searching for a topic.


Why do you think you would find explanations for all OCA chapters in this forum? First of all, this forum is not related to any of the available study guides. So even people who decide not to use a study guide can ask their questions in this forum. Secondly most of the topics in this forum do not have any reference to the chapter. And if they do, it is not really standardized (e.g. you have mentioned "chapter 2" in your post, someone else might use "CH02" in the subject,...) making it harder to find all different topics related to a specific chapter. Thirdly, not every study guide uses the same structure (chapters): chapter 2 from study guide A might cover access modifiers, but in study guide B it's all about control flow. So executing a search will give inconsistent results.

Probably the best thing you could do, is create bookmarks of all useful threads you like. And you can give a name to each bookmark, so you can add the chapter number in front of the topic subject. So in the end you'll have a nice overview of useful threads and the corresponding chapter number from the Sybex OCA study guide.
+Pie Number of slices to send: Send
Thanks ROel. WIll do that .
The knights of nee want a shrubbery. And a 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 1827 times.
Similar Threads
why is this an infinite loop?
Passed the OCA 8 Exam
OCA 8 Doubt
Sybex OCA 8 - Is it a typo (Runtime exceptions can be thrown in any method) ??
Maybe error in chapter 3, question #24 (Java OCA 8 Programmer I Study Guide)
OCA Java SE 8 Programmer I Study Guide: Exam 1Z0-808 by Jeanne Boyarsky & Scott Selikoff
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 04:25:41.