No: this is an abusive generalization. The whale shark (Rhincodon typus) practices swimming ventilation (ram ventilation), which requires it to move constantly, but many other sharks, particularly those lying on the bottom, breathe by active oral pumping without ever swimming.
Two distinct respiratory mechanisms
In the whale shark, water enters through the wide open mouth and passes passively through the gills thanks to the forward swimming movement alone, a system effective for this filter which travels tens of kilometers per day in open water. Without movement, water would stop circulating over the gills and oxygenation would stop: this species therefore cannot remain still for long periods of time.
But this mode is not universal among selachians. Many benthic species, such as nurse sharks or some carpet sharks, have oral and pharyngeal muscles capable of actively pumping water through the gills, much like a muscular pump, even when completely stopped on the bottom. This active oral pumping allows these species to rest, hunt on the prowl or hide in the sand without suffocating.
A preconceived idea to qualify
The statement “sharks must swim constantly to breathe” therefore only applies to some species, mainly large pelagic swimmers such as the whale shark, the mako shark or certain hammerhead sharks, for whom ventilation by swimming is obligatory or dominant. In species with a benthic lifestyle, oral pumping is sufficient to ensure gas exchange at rest. Certain species also combine the two strategies depending on their activity, alternating active swimming and immobility ventilated by pumping. The biology of the whale shark therefore illustrates a particular case, not a general rule for all of the approximately 500 species of sharks recorded.