This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

A question about review question in Chapter 2

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I have a question about one of the review questions in Chapter 2
question 9: how many times will the following code print "Hello World"


the answer is this is a infinite loop, but I thought it was going to print out Hello World 10 times. I have a hard time to understand the answer explanation.

My idea is that i is 0 at the beginning, then turn to 1, print Hello World first time, go back to for(), check i still less than 10, then, i turns to 2, print Hello World again.

Thank you all,
 
Ranch Hand
Posts: 186
1
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Matthew Han wrote:Hi guys,
I have a question about one of the review questions in Chapter 2
question 9: how many times will the following code print "Hello World"


the answer is this is a infinite loop, but I thought it was going to print out Hello World 10 times. I have a hard time to understand the answer explanation.

My idea is that i is 0 at the beginning, then turn to 1, print Hello World first time, go back to for(), check i still less than 10, then, i turns to 2, print Hello World again.

Thank you all,



The reason it's an infinite loop is for a couple of reasons. Look at the for loop, typically speaking its structure is as follows: for (initiallization; conditional statement; incremental statement). The constructed loop that you linked has for i = 0 which is where i starts, i < 10 , however where is the incremental test. then on the next line of code, it says i = i++ so i = i but then increments by 1 when it is done executing the System.out.println() statement.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic