As suggested by PeterH, I've changed some of the code
No, you didn't change it as he suggested. Read the post again, particularly around the reading of the serial port.
If the character read is global and therefore persistent, then once read, it will trigger the appropriate action every time through "loop ()" until a new character is read.