. You want to break out of the loop with
. Then,
the loop, not within it, you want to output your results.
Take some lines of the source file and walk through a printout of the code, using your fingertips. Ask yourself the questions that you ask the computer to make, and do what you have told the computer to do in response to them. The flaws in your present design will quickly become clear to you as they will best with no other way.