First of all, it's worth to mention that engine gimbaling is not the only way to control a rocket. You can use differential thrust, or vernier thrusters, or even aerodynamic flight control surfaces. But you are right on that, that all of these systems require some sort of control.
The gimbaling angles can't be pre-programed, because they have to dynamically adapt to minute changes in the vehicle's attitude. (That is the word used to describe where the nose is pointing.) If, during ascent, a sudden gust of wind tips the rocket to the left, it has to counter-act with something which creates a rotating moment in the other direction.
Therefore the rocket guidance system works in a loop. It senses the current attitude. Compares that to a target value. Actuates in such a way that the error decreases. Then repeats. This is called closed-loop control. (As opposed to the open-loop control, what would be what you described, with the pre-programed gimbal angles.)
If you are interested in this topic, there is quite a bit detail about the Saturn V Instrument Unit on wikipedia. That's the part of the Saturn V rocket which among other things, provided the guidance signals to the engines. http://en.wikipedia.org/wiki/Saturn_V_Instrument_Unit