This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Infinite for loop....??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Infinite for loop....???" Watch "Infinite for loop....???" New topic
Author

Infinite for loop....???

Naveen Raja
Greenhorn

Joined: Jul 28, 2006
Posts: 10
Hi All,

Can you please tell me how the following for loop can be a infinite loop..

for(int i=0;i<=012;i=i++)
{
}

I think the whole point is increment operation on i.When exactly it happens...??

Thanks a lot
Cheers,

Naveen

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

The whole point is that i=i++ is equivalent to i=i.
i gets evaluated before being incremented.

Try this :


[My Blog]
All roads lead to JavaRanch
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Read this one from the FAQ: Post increment operator and assignment

By the way, can you guess what this code will print?

I bet you will be surprised with the output.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Infinite for loop....???
 
Similar Threads
for loop not compiling
Infinite looping in 'for' and 'while' loops
Background Sound
load on start up
Array Project Help