Sometimes I see stuff like this:
X said, "..."
"..." X did this, and then said, "..."
Is it used to just vary sentence structure? Or to make dialogue more readable or smoother in certain situations?
Yes to both as Lauren said, and once you train the reader that you're a nice author who doesn't ever violate "One speaker per line" and sticks to that while mixing emotes and actions in too, you may go with a third form which is then even more smooth:
It's the actions that describe the speaker, and you kill two birds with one stone, give variety against "he said, she said" and get the scene to live for real.
Yes to both. Repetitive sentence structure grates on the ear. Also, sometimes it helps to clarify the speaker before the dialogue, and sometimes you actually want action or a pause to occur before the speech, but not separate from it.
The second example just inserts the tag in the middle of speech, which is fairly standard if redundant. The first example though...
If you're asking for the reason for this ordering - it's a matter of personal style. In literary writing it feels awkward and is rarely used... mostly by unskilled authors, because they don't know any better.
Now first, why they (the unskilled authors) write it like that, and when would skilled authors use this is how natural speech follows:
Contrast it with what you'd find in books:
If, in normal conversation, you report someone's speech, you first identify the speaker, then follow up either with the quote or a paraphrase. So, if you want to create this impression - of the narrator talking directly to the reader, then you'll use this ordering. It contrasts standard literary narration with conversational tone, removes the impersonal distance of the narrator.