• 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
  • 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

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

 
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might find this useful.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Nithya Mohan anand
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Joe!!.

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


NIthya
 
Nithya Mohan anand
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Nithya Mohan anand
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ROel. WIll do that .
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic