Breaking an Infinite Loop

Page 1 of 1

5 Replies - 11490 Views - Last Post: 22 October 2007 - 11:52 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=35858&amp;s=aff1b8c35f24d61268ea923716ff7a18&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 elboosta

Reputation: 0
• Posts: 2
• Joined: 22-October 07

Breaking an Infinite Loop

Posted 22 October 2007 - 06:55 AM

Hello, I use C++ to solve differential equations that are formatted similar to what is shown below:

```int main()
{
while (error>0.001);
{
OldVelocity = NewVelocity;
NewVelocity = ((Yhigh - Ylow)/DeltaX) + OldVelocity;
error = (OldVelocity - NewVelocity)/NewVelocity;
}

return 0;
}
```

The equations I'm solving cannot be solved analytically - only numerically. In order for this to be done, they are converging to some acceptable answer (based on error between adjacent results). Sometimes after I execute the program I see a mistake or realize that my answer will never converge. I have programmed before but I am new to C++. I know in MatLab if you are stuck in an infinite loop you can hit Ctrl+C and the computer will break out of it. Is there a command to do this in C++ so if for some reason my computer is stuck in an infinite loop I can break it out???

If not should I just put a counter in my loop, and if it excedes a certain number the loop is terminiated regardless of the convergence criterion?

Thanks.

Is This A Good Question/Topic? 0

Replies To: Breaking an Infinite Loop

#2 csmanoj

Reputation: 11
• Posts: 150
• Joined: 06-August 07

Re: Breaking an Infinite Loop

Posted 22 October 2007 - 06:59 AM

Ctrl + Pause/Break

• g+ + -o drink whiskey.cpp

Reputation: 249
• Posts: 13,507
• Joined: 12-July 02

Re: Breaking an Infinite Loop

Posted 22 October 2007 - 07:28 AM

The manual break condition wold actually be somewhat dependant on what operating system/platform you are using. What are you using?

#4 elboosta

Reputation: 0
• Posts: 2
• Joined: 22-October 07

Re: Breaking an Infinite Loop

Posted 22 October 2007 - 07:42 AM

Right now I am writing all of the programs on computer with Windows XP. For higher order non-linear diffy-q's and finite element code (which I haven't gotten into yet) I will run the programs on a machine with Linux.

#5 no2pencil

• Professor Snuggly Pants

Reputation: 6179
• Posts: 29,756
• Joined: 10-May 07

Re: Breaking an Infinite Loop

Posted 22 October 2007 - 07:51 AM

To break out of infinite loops, I usually set a range, test for that range, & then manually set the test to break the loop.

```int loopcount=0,max_loopcount=999,loop_err=0;
while(error>0.001) {
if(loopcount==max_loopcount) {
error=0;
loop_err=1;
}
loopcount++;
}

if(loop_err==1) {
printf("Possible infinite loop\n");
}

```

This post has been edited by no2pencil: 22 October 2007 - 07:51 AM

#6 jjhaag

• me editor am smartastic

Reputation: 48
• Posts: 1,789
• Joined: 18-September 07

Re: Breaking an Infinite Loop

Posted 22 October 2007 - 11:52 AM

For recursive/iterative numerical methods, you should ALWAYS have a builtin break condition, either like a static maxEvals variable, or a convergence test. Sometimes actually figuring out what the criteria should be is the most difficult part, however.

-jjh