Interfacing GSM module code optimization

6v6gt:
However, if the OP wants to test the return code from each command and immediately continue with the next command as soon as he gets the expected return code, then he has more work to do.

I would strongly recommend against integrating the serial input code and the serial output code. It may put the operations in order when you can do just one thing at a time with delays but it would make no-delay operating into a mass of spaghetti code.

The input code should run in every loop() by itself to receive serial at any time, buffer or process the char and set status for the output code to pick up on. The output code should run all the time to act on serial input. Neither should be inside the other.

The many do-many-things-at-the-same-time code tutorials give us techniques to do these things in small examples. Learning those also helps see how they go together. Working through some of those can help the coder with bigger projects.