English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

2 answers

I'll leave it to somebody else to give you the best looking guy or girl. I'd tell you the best looking girl out there is my wife, but as she isn't high profile I'll leave it to somebody else.

A loop in computer terms (or in math when speaking of algorithms) is when a specific block of code repeats a number of times. For example:

Let x be the value 100
While x < 200, do the following
...Write the value of x to the screen
...Add one to x

This basic algorithm will write the numbers from 100 to 199 to the screen. It is a loop, but not infinite.

Let x be the value 100
While x < 200, do the following
...Write the value of x to the screen
...Add one to y

This is a classic accidental infinite loop. It will print out '100' forever, because the loop modifies y, not x. Infinite loop.

Your question is how to check to see if a loop is infinite. Loops are usually defined by a boolean condition. If that condition cannot become false, the loop is infinite.

Sometimes it the condition can't become false because of poor programming. For example, a loop is told to read a file until it reaches the word "END". If the file is corrupt and it never finds the word "END" it'll keep trying to read the file over and over. The operating system will keep telling it it can't read any further, but it'll keep trying anyways. In cases like this, programmers generally leave themselves alternate exit conditions, like reading until it reaches the word "END" or trying to read further returns an error.

There's no sure way to find out if a loop is infinite or not. You could have a loop that keeps going until the user clicks the. Is it an infinite loop? No, because at any time the user can hit the mouse. But if the user has a heart attack and dies, then it's infinite. But that can't be detected.

The standard alternative is what's called a "time out interval." Take a communications program. It goes out to a web address and tries to get some data. It sits in a loop waiting for data to come back. But if it sits in that loop for more than 10 seconds, it "times out" and gives up. If it had waited 11 seconds it may well have succeeded, but the programmer decided that 10 seconds was enough to decide that it wasn't going to happen.

So if you're in a situation where you can't determine if your loop is infinite or not (that is, can't determine if the condition you're checking can ever become false) you can add a time out value and break out of the loop after a certain amount of time. But for 99% of the loops you'll ever write, just carefully putting the loop together is sufficient.

Hope this helps.

--------------
Relevant Computer Science joke:
Q: How did the programmer die in the shower?
A: The shampoo bottle said "Wash, rinse, and repeat"

2006-06-22 06:06:46 · answer #1 · answered by Mantis 6 · 1 0

i think the gal
actually An infinite loop is a sequence of instructions in a computer program which loops endlessly
if u consider this in maths... if it is infinite..how can we consider that as a loop..bcos the line can never form a loop if we go on drawing..ex: take a very big ELLIPSE u cant complete it

2006-06-22 03:56:10 · answer #2 · answered by Prakash 4 · 0 0

fedest.com, questions and answers