By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 712 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

how can we detect infinite loop in our program?

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

thanks
Aug 15 '17 #1
Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
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.
Aug 15 '17 #2

Frinavale
Expert Mod 5K+
P: 9,731
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.
Aug 17 '17 #3

Expert 100+
P: 2,396
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.
Expand|Select|Wrap|Line Numbers
  1. for (i=0; i<10; i++) { ... }
Aug 17 '17 #4

Post your reply

Sign in to post your reply or Sign up for a free account.