Infinite Loop

Infinite looping occurs when we aren't making progress towards loop termination. There are several situation when we will end up with an infinite loop. The example below illustrates the problem where we are trying to display the sum of all numbers from 5 to 10.

int i = 5;
int sum = 0;

while (i <= 10)
   sum += i;

We will end up with an infinite loop because we did not increment the counter for the loop. The counter remains the same through each iteration of the loop and progress is not made towards loop termination. The correct code is shown below.

int i = 5;
int sum = 0;

while (i <= 10) {
   sum += i;
   i++;
}

Fook Yuen Sin - Team 1