You are here

Infinite Loops

10 February, 2015 - 12:19

At this point it's worth mentioning that good programming always provides for a method to insure that the loop question will eventually be false so that the loop will stop executing and the program continues with the next line of code. However, if this does not happen then the program is in an Infinite loop. Infinite loops are a bad thing. Consider the following code:

Example 15.2: C++ source code: infinite loop

for (counter = 0; counter  <  5;)   {   cout « "\nI love ice cream!";   } 

The programmer assigned a value to the flag during the initialization step which is correct. However, he forgot to update the flag (the update step is missing). Every time the test expression is asked it will always be true. Thus, an Infinite loop because the programmer did not provide a way to exit the loop (he forgot to update the flag).