Probably Djneo posted a strict PDL description than a real pseudo code logic (sorry, but the posted idea itsn't logic for me).
Personally I think the problem is not only in the last condition (while loop) but whole presented code. What about
cpt3 and
cpt4 values? They are actually doing nothing.
That's all from me in this thread since Startrek related problems are far too much for my poor brain

.