Computing.Net > Forums > Programming > Infinte loop

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

Infinte loop

Reply to Message Icon

Original Message
Name: Apocalypse
Date: July 4, 2003 at 06:30:49 Pacific
Subject: Infinte loop
OS: WIN ME
CPU/Ram: p3 & 128mb
Comment:

i Often encounter this problem.....

Suppose there are two nested infinite looops....then what is the best method used to get out of the innermosr infinite loop..

Thanx


Report Offensive Message For Removal


Response Number 1
Name: Rolos
Date: July 4, 2003 at 06:50:05 Pacific
Reply: (edit)

Is this case even possible? I mean if the exterior embedded loop is infinite, you can't possibly go into another infinite loop because the condition in the current infinite loop will hold true forever. Therefore making it impossible to go into or even checking the condition of the next loop ever.

Seriously though, I would like to see two loops looping infinitely at the same time. I'm also very certain that the world to come to an end, fire and brimstone would fall from the sky and Osama Bin Laden would be considered a great American hero. Does this sound right to anyone? Maybe it's just me and it's a bit too late in the evening.

Sorry for goin off in your thread *Apocalypse*. The hatred is directed at the topic, not at you. Did by chance you mean that the innermost loop was infinite and not the outer loop instead of both?

-Rolos

P.S. If by chance what I said in this thread makes absolutey no sense to anyone, by all means feel free to bombard me with insults for being a total idiot.


Report Offensive Follow Up For Removal

Response Number 2
Name: George
Date: July 4, 2003 at 07:00:58 Pacific
Reply: (edit)

Makes perfect sense.

An infinite loop no matter if its an inner nested lop or the outer master loop, is called a lock up.

Normally to exit any infinite loop requires the "three-finger-salute" or that little reset button on the front.

Sorry Apocalypse, that was meant to be a tongue in cheek. :^)

To exit from any loop requires:
completion of a condition.

for x = 1 to 100 {condition}
for y = 1 to 1000 {condition}
Print y
if y = 32 then exit {condition}
next y
Print x
if name="sam" then exit {condition #4}
next x

Condition 4 may make no sense but it is a condition, this allows early "breakin-out" of
of a loop.

I hope this helps.
George



Report Offensive Follow Up For Removal

Response Number 3
Name: Apocalypse
Date: July 4, 2003 at 07:09:14 Pacific
Reply: (edit)

U wrote....to exit when a particular condition satisfies...right....but if oone wants to come out of the whole thing at that very particular instant then what to do ??


Report Offensive Follow Up For Removal

Response Number 4
Name: Don Arnett
Date: July 4, 2003 at 07:31:16 Pacific
Reply: (edit)


Did I miss the mention on what language we are talking about here??


The method is the same whether the loops are infinite or not. As George said, to early exit from a loop requires an if statement to check a desired condition and if the condition is true, whatever statement is required for the particular language to get out of the loop.

In C/C++, the early exit statement is 'break'.

if (doneEarly == TRUE) {
break;
}

Also, agreeing with Rolos, nested infinite loops don't make sense. Consider an infinite loop with a nested infinte loop, neither with early exit statements. The outer loop will start and then the inner loop will start and the inner loop will run infinitely, so the outer loop will never move on to it's second iteration, because the inner loop never completes, so what is the point of the outer loop?

Now, move on to the case where the inner loop has an early exit check. My original thinking was to say that if the inner loop had an early exit check, then why not just make the early exit check the loop condition?

But, I suppose there could be a situation where you have a long inner loop and the early exit check 'fits' in the middle of the loop rather than at the end, so making it the loop condition would be 'inconvenient'.

But, I think that I'd still recode it so that it's the loop condition rather than having two infinite loops. This way, when someone else looks at the code, it'll be more obvious how you exit the inner loop.


Report Offensive Follow Up For Removal

Response Number 5
Name: Don Arnett
Date: July 4, 2003 at 07:35:50 Pacific
Reply: (edit)

After posting my previous response I looked at the original post again and noticed the leadoff statement "i Often encounter this problem....."

I also noticed the name of the person posting.

It makes more sense now... ;)

(not commenting on A's coding skills, just commenting on how the topics of 'end of the world' and infinite loops seem to go well together)


Report Offensive Follow Up For Removal


Response Number 6
Name: Don Arnett
Date: July 4, 2003 at 07:47:15 Pacific
Reply: (edit)

In A's second post, are you saying that you want to exit both loops at 'that particular instant'?

If you want to simply exit the program without doing anything else, in C/C++ you'd use exit().

If you don't want to exit the program, you'd put an early exit check (maybe the same one) in the outer loop immediately following the end of the inner loop.

Or, inside the inner loop, when the early exit condition is true, just before you 'break' you'd set a flag and then check that flag in the outer loop immediately after the end of the inner loop.


Report Offensive Follow Up For Removal

Response Number 7
Name: Apocalypse
Date: July 4, 2003 at 08:32:48 Pacific
Reply: (edit)

U see ill tell u :

when i was making a game called battleship
i randomly created 5 ships of varying length across a 10*10 matrix.....so while doing so i had to go in multiple looping....infinite because i really didnt know how many times......then since i didnt get any idea .....i used goto .....since it is not recommended i asked......

Thanx


Report Offensive Follow Up For Removal

Response Number 8
Name: Infinite Recursion
Date: July 4, 2003 at 10:52:11 Pacific
Reply: (edit)

I believe I was too late on gettin this post :( But, I don't feel so bad, because Don hammered it to death for me. ;)

Infinite Recursion

ps: nested infinite loops, running at the same time? the world is comming to an end.


Report Offensive Follow Up For Removal

Response Number 9
Name: eaw8806
Date: July 6, 2003 at 17:24:24 Pacific
Reply: (edit)

yea, wasnt that like the basic loop rule? only one loop can be running at a time? like html tags... (HTML) (BODY) (/BODY) (/HTML)...it cant ever be (HTML) (BODY) (/HTML) (/BODY)
same with loops. its interesting though, that 'infinite recursion' has only made one post about, well, infinite recursions :)



Report Offensive Follow Up For Removal






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you own an iPhone?

Yes
No, but soon
No


View Results

Poll Finishes In 6 Days.
Discuss in The Lounge
Poll History




Data Recovery Software