Infinite Loop
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Ed_Kueh
Posted On:   Sunday, February 24, 2002 11:17 AM

I have seen many author/programmer use infinite loop without a break to exit the loop when the exit condition arises.
My question is what makes the condition un-true ? Does EOF makes the condition un-true ? If so, I have also seen a socket stream being read this way.
Thank you.

eg. (a file is wrapped inside a bufferedReader obj)

while (true){
String s = aBufferedObj.readline( );
System.out.println (s);
}

Re: Infinite Loop

Posted By:   Luigi_Viggiano  
Posted On:   Sunday, February 24, 2002 02:48 PM

>I have seen many author/programmer use infinite loop without a break to exit

This is because there are many author/programmer with bad habits.

Good C programmers are often bad Java programmers...often they fall in "legacy" bad practices, like this, or crypting coding etc.



(a file is wrapped inside a bufferedReader obj)
while (true){
String s = aBufferedObj.readline( );
System.out.println (s); }
}

Only an exception, when the file ends, can break this loop. Generally it's better to check for the end of file, instead of causing an exception.

Never-ending loops have sense for daemon threads, or for processes that never end (like for example a shell program reading commands from the keyboard and executing it by the OS). In some occasions, this kind of loops are ok, but in most cases it's just a bad practice to avoid.

About | Sitemap | Contact