ENDLESS LOOPS


One important aspect of looping to be aware of 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).

Hint 

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

Trick 

If, white testing the execution of your Visual C++ application during development, you think you might have an endless Loop, you can stop the program from running by opening the Debug menu and clicking on the Stop Debugging menu item.

If you are running a compiled Visual C++ application from the Windows desktop and you think it might 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 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:

 Int32 intCounter = 0; while( intCounter < 5 ) {   intCounter = intCounter - 1;   MessageBox::Show( intCounter->ToString() ); } 

In this example, a while loop is established 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, the programmer here accidentally decremented the value of intCounter by 1. As a result, the value of intCount will continuously decline, never reaching 5. As a result, the loop will never terminate on its own. Always double-check the way that you have set up your loops, and test your applications thoroughly so that if you accidentally introduce a situation where an endless loop can occur, you'll catch it.




Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner 2006
Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner 2006
ISBN: 735615381
EAN: N/A
Year: 2005
Pages: 131

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