• Post Reply Bookmark Topic Watch Topic
  • New Topic

infinite for loop question  RSS feed

 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, in the book OCA: Oracle®Certified Associate Java®SE 8 Programmer IStudy GuideExam 1Z0-808,
page 96 I encountered the following question which confused me:


How many times will the following code print "Hello World"?

A. 9
B. 10
C. 11
D. The code will not compile because of line 3.
E. The code will not compile because of line 5.
F. The code contains an infinite loop and does not terminate.


The correct answer is F that this loop is an infinite loop.
My questions are:
I understand that in the for loop it is optional to add the third field.
The thing that confuses me is that if we don't add the third field then the code compiles but every time it initializes i to 0?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
551
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ioanna,
It's actually more subtle than that. It only initializes i to 0 once. And the loop ends when i reaches 10.

This code does in fact print out "Hello World" 10 times and then stop:


Which tells us the problem is with


That line does three things:
  • Note the original value of i (which is 0)
  • Increment i (which makes it 1)
  • Store the noted value back in i (which is now 0 again)


  • You are correct that i never goes past zero. But it isn't the loop's fault!
     
    Ioanna Katsanou
    Ranch Hand
    Posts: 131
    1
    Java Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jeanne Boyarsky wrote:Ioanna,
    It's actually more subtle than that. It only initializes i to 0 once. And the loop ends when i reaches 10.

    This code does in fact print out "Hello World" 10 times and then stop:


    Which tells us the problem is with


    That line does three things:
  • Note the original value of i (which is 0)
  • Increment i (which makes it 1)
  • Store the noted value back in i (which is now 0 again)


  • You are correct that i never goes past zero. But it isn't the loop's fault!


    In order to understand this, I tried this:


    Why does it print out 0??
    I still can't understand this ! x = x++ doesn't mean that x increases by one, so it becomes 1 and then x equals 1???
    This is so confusing.. 

     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ioanna Katsanou wrote:. . . doesn't mean that x increases by one, so it becomes 1 and then x equals 1???
    This is so confusing.. 
    Don't worry; everybody else gets confused by that too.
    Yes, x does become 1, but you cannot see that in that line. All you can see there is the value of the whole expression x++ which is equal to 0. Yes, there are two different values. Then you reassign the 0 to x, so it prints 0.
     
    Paweł Baczyński
    Bartender
    Posts: 2085
    44
    Firefox Browser IntelliJ IDE Java Linux Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    x++ does increase x by one.
    What is tricky is that the value of the expression x++ is the value before incrementing.

    x = x++ could be broken into:
    temp = x;
    x = x + 1;
    x = temp;
     
    Jesper de Jong
    Java Cowboy
    Sheriff
    Posts: 16060
    88
    Android IntelliJ IDE Java Scala Spring
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Have a look at this CodeRanch Wiki page: Post Increment Operator And Assignment
     
    Ioanna Katsanou
    Ranch Hand
    Posts: 131
    1
    Java Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ok thanks

    I got it !!! this was quite confusing thank you all
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!