Skip to content
SPM Tuition
Computer Science · Program structure and debugging

Loops that stop early or never stop

Your program either stops after the first few values or freezes and never finishes.

A loop that misbehaves usually fails one of three checks: the starting value, the condition, or the update. Tracing the condition on every pass shows which one is at fault.

This lesson is part of program structure and debugging. The basic limits of loops are covered in writing loops with correct boundaries.

Example 1: a loop that never ends

The program should count down to zero in steps of 3, printing each value.

SET n = 10
WHILE n <> 0
  OUTPUT n
  SET n = n - 3
END WHILE
Pass n before n <> 0 n after
1 10 TRUE 7
2 7 TRUE 4
3 4 TRUE 1
4 1 TRUE −2
5 −2 TRUE −5

The values are 10, 7, 4, 1, −2 and so on. The value 0 is never reached, because the step of 3 jumps over it, so n <> 0 is always true. The loop never ends.

The fix is to change the condition to n > 0. Now the loop prints 10, 7, 4, 1 and stops when n becomes −2.

Example 2: a loop that stops too early

The program should add five marks, then output the total.

SET total = 0
INPUT mark
WHILE mark > 0
  SET total = total + mark
  INPUT mark
END WHILE
OUTPUT total

The marks are 70, 0, 55, 80 and 90. A student scored 0 in the second paper, and that is a real mark.

Step mark mark > 0 total
Read first 70 TRUE 70
Read second 0 FALSE 70

The loop ends and outputs 70, but the correct total is 70 + 0 + 55 + 80 + 90 = 295. The stop condition treats a valid 0 as an end signal.

The fix is to count the marks instead. Use FOR i = 1 TO 5, which always reads exactly five values.

How do you diagnose any loop?

Run three checks in order.

  1. Start: does the variable begin at the right value?
  2. Condition: can it ever become false, and could it become false too soon on valid data?
  3. Update: does the body move the variable toward the stop?

A mistake to avoid is fixing the first symptom you notice. Always trace at least three passes before changing anything.

Check yourself

Why does this loop never end, and how do you fix it?

SET x = 1
WHILE x <> 10
  SET x = x + 2
END WHILE
Answer

The values of x are 1, 3, 5, 7, 9, 11 and onward, all odd, so x never equals 10. The condition x <> 10 stays true forever.

Change it to WHILE x < 10. The loop then stops when x reaches 11.

What to study next

Next, learn to tell a typing fault from a design fault in distinguishing a syntax error from a logically wrong result. The restricted pseudocode trace trainer offers more loop traces.

If you want a teacher to go through your own loops, see online one-to-one Computer Science tuition.

Common questions

What causes an infinite loop?

The condition stays true forever. Usual reasons are that the variable in the condition is never updated, it moves in the wrong direction, or it jumps past the exact value the condition waits for.

Why would a loop stop too early?

The condition becomes false before all the data is processed. For example, using a legitimate value such as 0 as the stop signal means a real zero ends the loop by mistake.

Is <> safer than < in a loop condition?

Not always. A condition such as n <> 0 works only if n lands exactly on 0. If the step can jump past 0, use n > 0 so the loop stops whatever the step size.

How do I trace a loop that never ends?

Trace until a pattern repeats or the values move away from the target, then stop. You do not need infinite rows; four or five are enough to show the condition will never become false.

If a frozen or short-running loop leaves you guessing, a one-to-one Computer Science teacher can trace it with you and show which of the three checks fails.

  • Online one-to-one lessons for your child with an experienced teacher.
  • Your first class is a one-hour trial, from RM50. The fee is agreed before you book.
  • Happy with the teacher? Continue with lessons of about 1.5 hours. If not, ask for another teacher.