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.
I really love the dialogue in the Guest translation of the Mabinogion, it adds to the feeling that you are reading a text from a different time:
No variation, and only the first speaker is named, the following turns are all not identifed.
It won't help you write good dialogue today, but it shows that there is not one single genre of fiction with universal rules, and that maybe sometimes a bit of courage to find your own style of writing will make your book stand out and prevail among a host of formulaic bestseller clones.
I use non-conversation bits to show where pauses or lulls in the conversation occur. They can be short, like:
"Hey," said Bob.
"So..." Bob started and then cleared his throat. This was only my third week on the job but I knew even at that early date that when Bob cleared his throat, he had something truly stupid to say. "Do you have those TPS reports?"
I stared at Bob incredulously. What did he think this was, Office Space? "No, Bob, I do not have them. Perhaps they got lost in the year 1998."
Hopefully that first one demonstrated a little awkward pause while the main character thought up a (semi) witty response. You can also make them longer:
"Nancy, what do you think of the global dilemma?"
Patricia was always asking Nancy stupid questions like this. She was soooo dumb. If she really wanted an answer, couldn't she just look it up on like Wikipedia or something? Maybe she could ask Jeeves. Oh my god! Was Ask Jeeves even still around? Maybe Jeeves had been replaced by a younger, hunkier guy named like Falco or something. No, that was stupid; computers don't get old and only rich guys ever used Ask Jeeves so they'd probably always want some fake old butler dude.
"Nancy, are you even listening to me?"
You can just add he said/she said to the end of each line; in fact, your readers will generally get the hang of it after an exchange or two (especially if your dialogue is strong enough) and you really don't need to add any tags at all. The thing about doing that is, it makes the conversation feel like a high-speed, no-pause madcap comedy style banter (think: Gilmore Girls), and if your characters are just talking about what went on that day at school it's going to feel a bit off. Additionally, there becomes a risk of making the characters seem like disembodied talking heads if you keep it up for too long.
One little trick you can do - that I recommend doing for dialogue in general - is on your subsequent drafts, go in and read it out loud. For one thing, in my experience this really roots out any inauthentic sounding verbiage, sentences that are too long to be said within one breath, and so on. For another, I think that with dialogue in particular you get a pretty good sense of how long those filled pauses occur. In real life, people say "um" and "ah" a lot; readers won't like you to drop those in (unless you want to portray a dullard) but they do kind of expect the "pause" side of the filled pause.