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?
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!
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.