ENDLESS LOOPS


One of the concerns to be aware of when working with loops is an endless loop. Endless loops occur when you apply faulty logic in the creation of a loop that results in the loop running forever (or until you forcefully terminate your application).

image from book
DEFINITION

An endless loop is a loop that has no means for terminating, causing the loop to run forever and draining computer resources.

image from book

Trick 

If, while testing the execution of your Visual Basic application during development, you think you may have an endless loop, you can stop the program from running by opening the Debug menu and clicking on the Stop Debug menu item.

If you are running a compiled Visual Basic application from the Windows desktop and you think it may be caught in an endless loop, you can forcefully terminate its execution by pressing the Ctrl + Alt + Delete keys simultaneously. This opens the Task Manager window, from which you can then select your application and click on the End Task button.

The following example demonstrates how easy it is to accidentally set up an endless loop.

 Dim intCounter As Integer = 0 Do While intCounter < 5   intCounter = intCounter - 1   MessageBox.Show(intCounter) Loop 

In this example, a Dowhile loop is set up to iterate as long as the value of intCounter is less than 5. However, instead of incrementing the value of intCounter by 1 upon each iteration of the loop, I accidentally decremented the value of intCounter by -1. As a result, the value of intCount will never reach 5 and the loop will never terminate on its own. The lesson here is to always double-check the way that you have set up your loops and to test your applications thoroughly so that if you do accidentally introduce a situation where an endless loop can occur, you'll catch it.




Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner
Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner
ISBN: 1592008143
EAN: 2147483647
Year: 2006
Pages: 126

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net