Why does adding an 'if' effect my state machine?

Try println instead - it's concatenating the print at the top of loop.