Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How many ways to get into an endless loop? 2

Status
Not open for further replies.

GwydionM

Programmer
Oct 4, 2002
742
GB
I've done one - you have a loop that is to be done 7 times, then it gets raised to 15. But it's a language where you define your own data field and you forget to increase it to two digits, so it loops from 9 back to 0.

I also heard of a case where a man set his e-mail to tell anyone who wrote that he'd moved. This got into an endless loop with someone else who'd got an automatic message to say he was on holiday. This may be apocryphal and would certainly be very early - Outlook tells you just once, in my experience.

------------------------------
An old man [tiger] who lives in the UK
 
Arguably the most trivial one:
Code:
open recordset
Do While not rs.Eof
	do something
	
	forget rs.moveNext
Loop
Then you restart IIS, kill VB program or whatever :p That's why some people write methods/functions a la nextRecord() doing skip & retrieval all at once, as seen in PHP.

Another example of infinite loops might happen on web pages using onblur DOM event. N00bs like it a lot because of simplicity... it fires like crazy though. Throw in some focus() or fireEvent() calls and get ready for 100% CPU utilization.

------
[small]<this is sig>
select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')
</this is sig>[/small]
[banghead]
 
endless loop

void JobHunting(void)
{
while(ApplyForJob()==NOREPLY)
Depression++;

return;
}



If somethings hard to do, its not worth doing - Homer Simpson
 
Doozer you have a return in a void function perhaps this is the problem?
I notice that depression is not local, as the rest of us in TTUK know.
Look! someone just give him a job.



Steve: Delphi a feersum engin indeed.
 
force of habit steve. (some compilers complain if you dont return from a void function)

and yes global Depression.

also incremented in
void SocialLife(void)
and
void LoveLife(void)

If somethings hard to do, its not worth doing - Homer Simpson
 
Recursive procedure calls are also a nice way to loop endlessly.

Something as simple as

Public sub buzz()
call buzz
end sub

 
>>>> Recursive procedure calls are also a nice way to loop endlessly.

That will not actually loop forever, because you will blow the stack.

Simplest endless loop in Java/C#/C/C++ etc is :

Code:
  while (true)

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
don't forget
Code:
for (;;) {}

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
Recursive endless loop in Java(not tested)

Code:
public void foo() {
  try {
   foo();
  catch (StackOverflowError e) {
   foo();
  }

Cheers,
Dian
 
Here's a nastly little hack but just as effective.
Code:
#!/usr/bin/perl

# always one step ahead 
for ( $i = 0; $i < ($i + 1); $i++ ) { }

M. Brooks
 
These two people with the endless e-mail loop weren't Lyndon and Dyfrig by any chance?!?!

A computer always does what you tell it to, but rarely does what you want it to.....
 
there's an infinite number of ways to get into a endless loop...

--------------------
Procrastinate Now!
 
Any programmer can see lots of ways to intentionally create a loop. What I was hoping for was more cases where you could create one while writing code that looked sound at first glance.

------------------------------
An old man [tiger] who lives in the UK
 
You mean like this!
But I havn't tested it.

Code:
a = 10;
for(;;);
   {
   a--;
   if (!a) break;
   }



Steve: Delphi a feersum engin indeed.
 
Code:
Dim I As Long
I = 1
Do
   I = I ^ 1.1 + 0.49999
   Debug.Print I
Loop Until I > 10000

Maybe not so good a choice of data type, eh?
 
Off topic, but we get a nearly endless E-mail loop via reply all responses to information E-mail's sent Agency-wide (10,000 + Accounts).

Typical responses are

Do
1. "What's this?" 1 user
2. "Information only please don't reply." 10+ users
3. "Why am I getting this?" 2-3 users
4. "Quit replying to all it's inapropriate."20+ users
5. "Please remove me from this list it's clogging my inbox." 10+ users
Until several days pass or IT replies "We appolagize no has recieved E-mail for the last three days our servers were full. Please do not reply to subject E-mail and delete it form your inbox."

followed by my favorite
"I didn't" 1 user

Goto typical responses are


[thumbsup2] Wow, I'm having amnesia and deja vu at the same time.
I think I've forgotten this before.


 
Stop sending the email with the recipients in the To: list. Use Bcc: and the problem is solved.
 
Well, I would think most people code endless loops unintentionally. I know I've created endless loops when I was first playing with for loops and arrays.

I'd write something like this and a little mistake in the code could cause the program to loop forever. I also had to account for an error where the array's dimensions were 0 and it wanted to reduce it to -1. It was a really poor way to handle what I was trying to accomplish.
Code:
for i = 0 to UBound(SomeArray)
    if SomeArray(i) then
        if i <> UBound(SomeArray) then
            for j = i to UBound(SomeArray) - 1
                SomeArray(j) = SomeArray(j+1)
            next
        end if
        ReDim Preserve SomeArray(i-1)
        i = i - 1
    end if
next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top