Breaking an Infinite Loop

Can I break an infinite loop while it is running?

Page 1 of 1

5 Replies - 6067 Views - Last Post: 22 October 2007 - 11:52 AM Rate Topic: -----

#1 elboosta  Icon User is offline

  • New D.I.C Head

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

Breaking an Infinite Loop

Post icon  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  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 150
  • Joined: 06-August 07

Re: Breaking an Infinite Loop

Posted 22 October 2007 - 06:59 AM

Ctrl + Pause/Break
Was This Post Helpful? 0
  • +
  • -

#3 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 elboosta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 45
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1