In animal locomotion a tradeoff exists between stereotypy and flexibility: fast long-distance travelling (LDT) requires coherent regular motions, while local sampling and area-restricted search (ARS) rely on flexible movements. We report here on a posture control system in C. elegans that coordinates these needs. Using quantitative posture analysis we explain worm locomotion as a composite of two modes: regular undulations versus flexible turning. Graded reciprocal regulation of both modes allows animals to flexibly adapt their locomotion strategy under sensory stimulation along a spectrum ranging from LDT to ARS. Using genetics and functional imaging of neural activity we characterize the counteracting interneurons AVK and DVA that utilize FLP-1 and NLP-12 neuropeptides to control both motor modes. Gradual regulation of behaviors via this system is required for spatial navigation during chemotaxis. This work shows how a nervous system controls simple elementary features of posture to generate complex movements for goal-directed locomotion strategies.