how can we detect infinite loop in our program?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fayiz
    New Member
    • Aug 2017
    • 1

    how can we detect infinite loop in our program?

    i want to know can we detect infinite loop in our programs and yes how to do that.

    thanks
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You can start by putting a breakpoint at each do/while/for and run the program using the debugger. The execution will halt at the break point. All you need do is count the expected number of breaks and then remove the breakpoint. When all breakpoints have been removed you should be in good shape for loop control.

    BTW: If you are using oddball stuff like goto then this is a much harder problem to deal with.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Runtime: If your application never finishes a task, there is an infinite loop.

      Code: Look for a loop whose stopping condition isn't set. This could be because you aren't properly incrementing or decrementing a counter/index variable or you aren't setting something to true/false because a condition is never met.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        If loop termination is determined in part by user input then validate the user input before entering the loop. (Actually, you should always validate user input.)

        Don't modify the loop variable within the body of the loop. For example, don't alter i within the body of the following loop.
        Code:
        for (i=0; i<10; i++) { ... }

        Comment

        Working...