Fixes a bug introduced during switch from goto loop to for loop.
Closes #1267 (closed)
Closes https://gitlab.cern.ch/cta/operations/-/issues/801