in reply to
A way to avoid repeated conditional loops
Call it “a flag variable” if you want to, but this is a classic place for a finite-state machine (FSM) algorithm. I presume that you are familiar with the term.
Edit: One subtle distinction that sometimes comes in handy with FSM algorithms is to put “read the next line” as a subroutine-call that occurs within each state-handler, instead of structuring the algorithm within a while-loop as shown above. Sometimes you might want to loop through more than one consecutive state while processing the same line.