The four-day cycle of attacks that Iran set off with the United States over the Strait of Hormuz has risked derailing the newly reached ceasefire in a war both sides are eager to end. Yet for Iran, analysts say, it was a necessary gambit.