To navigate different environments, an animal must be able to adapt its locomotory gait to its physical surroundings. The nematode Caenorhabditis elegans, between swimming in water and crawling on surfaces, adapts its locomotory gait to surroundings that impose approximately 10,000-fold differences in mechanical resistance. Here we investigate this feat by studying the undulatory movements of C. elegans in Newtonian fluids spanning nearly five orders of magnitude in viscosity. In these fluids, the worm undulatory gait varies continuously with changes in external load: As load increases, both wavelength and frequency of undulation decrease. We also quantify the internal viscoelastic properties of the worm’s body and their role in locomotory ...