4 Replies - 560 Views - Last Post: 24 January 2012 - 02:36 PM Rate Topic: -----

#1 Nameo0  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 75
  • Joined: 30-November 09

Can someone advise me on how to deal with infinite loops.

Posted 24 January 2012 - 02:29 PM

I use JCreator to program in Java and once in-a-while I write a program that loops itself indefinitely. As a result of the loop I have to ctrl+alt+del and "End Task" JCreator.

I was wondering what is the best way to prevent against the indefinite loops?
Is This A Good Question/Topic? 0
  • +

Replies To: Can someone advise me on how to deal with infinite loops.

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10786
  • View blog
  • Posts: 40,175
  • Joined: 27-December 08

Re: Can someone advise me on how to deal with infinite loops.

Posted 24 January 2012 - 02:31 PM

Infinite loops are logic problems. All loops have conditions. As long as those conditions evaluate to true, the loop will continue. You can either use a break statement to break out of the loop, or change the variables so the condition evaluates to false.
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2816
  • View blog
  • Posts: 11,924
  • Joined: 20-September 08

Re: Can someone advise me on how to deal with infinite loops.

Posted 24 January 2012 - 02:32 PM

Increment a count variable in the loop that's otherwise infinite and break out of the loop after n iterations
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Can someone advise me on how to deal with infinite loops.

Posted 24 January 2012 - 02:35 PM

It would be helpful if you were to give us an example of one of your infinite loops.

But for a for loop of the general form:
for ( initialization; termination; increment )
{
    statement(s)
}

the loop variable should converge to the termination condition either due to the increment (or decrement) or due to a change caused by the statement(s) portion of the loop.
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Can someone advise me on how to deal with infinite loops.

Posted 24 January 2012 - 02:36 PM

The loop will iterate according to what condition you have given to it. So if you tell it to loop while x > y. This means it will loop until this condition fails.
So if you want to make sure you are not getting infinite loop, then you have to make sure there is a possibility that after some iterations, your condition will be false! That is the way to deal, so look at your code and think why my condition is not getting false? may be the variables are not updated in the loop or anything that prevent the loop condition variables from changing will cause an infinite condition.
For your special case, may be show us your code where the infinite loop was occurring, and we may be able to help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1