• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

infinite for loop question

 
Ranch Hand
Posts: 182
1
Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author & internet detective
Posts: 42103
933
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 182
    1
    Oracle Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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..  

     
    Marshal
    Posts: 80254
    428
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Bartender
    Posts: 2237
    63
    IntelliJ IDE Firefox Browser Spring Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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;
     
    Java Cowboy
    Posts: 16084
    88
    Android Scala IntelliJ IDE Spring Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Have a look at this CodeRanch Wiki page: Post Increment Operator And Assignment
     
    Ioanna Katsanou
    Ranch Hand
    Posts: 182
    1
    Oracle Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    ok thanks

    I got it !!! this was quite confusing thank you all
     
    You've gotta fight it! Don't give in! Read this tiny ad:
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic