Your question refers to how to pair dialogue (i.e., direct speech) and narrative.
In your first example, the following is correct:
Albert said, "I like to eat cake."
The narrative "Albert said" offsets the direct speech with a comma after "said." Normally, a comma doesn't separate a verb from its direct object when the direct object is immediately adjacent to it. An exception to that rule, however, is direct speech. Even though the direct speech "I like to eat cake" is the direct object of the verb "said," since it's direct speech, we use a comma to separate it. Other ways to properly write it would be:
- "I like to eat cake," Albert said.
- "I like to eat cake," said Albert.
In your second example, you wrote:
John said, "I have to go to the store and buy some milk."
This is likewise correct. That could also be written in structures similar to the two examples I gave above. By the way, notice that you have not put the comma in this example or in your first example inside the quotation marks, which is correct.
In your third example, you wrote:
"I have to go to the store," John said, "and buy some milk."
This, too, is correct. When you interrupt a sentence of direct speech midsentence with narrative, it is proper to offset the narrative with commas. You wondered why we put the comma after "store" in the quotation marks but don't the comma after "said" in quotation marks. The reason is in American English (AmE), we only include punctuation that trails direct speech inside the quotation marks, not punctuation that precedes or introduces the direct speech.
Additional Information:
One thing you didn't cover that you might like to know is what to do when instead of interrupting a sentence of direct speech midway with narrative, you put the narrative at the end of that sentence of direct speech but then wish to continue that direct speech with a new sentence. Here are a couple of examples of how you would write that:
- "I have to go to the store," John said. "I have to buy some milk."
- "I have to go to the store," said John. "I have to buy some milk."
Also, know that you don't always have to use the verb "say." In fact, using other verbs can help you be descriptive without having to use adverbs or adjectives, for example, you could write:
- "I have to go to the store," complained John. "I have to buy some
milk."
- "I have to go to the store," John droned. "I have to buy some milk."
Notice how in 1 above, the writer using the verb "complained" in lieu of "said" is able to convey John's tone and mood, convey he wasn't happy about having to go, all without having to laden the narrative with any modifiers.
Notice how in 2 above, the writer is able to convey that John was being monotonous and boring, possibly conveying the annoyance of who was listing to John at John just boringly prattling on about mind-numbing trivialities that didn't matter at all to who was listening, all without using any modifiers.
That doesn't mean you can't use modifiers or shouldn't use modifiers, for example:
- "I like to eat cake!" abruptly exclaimed Albert with oddly giddy
enthusiasm.
- "I like to eat cake?" wondered Albert aloud soliloquizingly.
- "I like to eat cake," Albert grumbled under his breath after not
being offered any.
Finally, know that narrative isn't always required when it's clear who is speaking, for example:
"I like to eat cake," said Albert, "but not without milk."
"I have to go to the store and buy some milk," responded John.
"Nah, I'll just skip the cake. Don't put yourself out."
"Well, I didn't mean now! But, OK. Whatever. I can go. It's no big
deal."
"Are you sure?"
"Yeah, it's fine. What's a birthday without cake, right?"
"Right. Thanks. Oh, and can you pick up a six pack of Natty Lite, too?"
Notice how in the above exchange, there is only narrative accompanying the first two instances of direct speech. In the five that follow, there's no narrative. It's not needed. That's because the narrative in the first two instances informs the reader of who said what. Being so informed and since conversations are back and forth, we can readily infer who said what from that point on without having to be told with narrative, like we can tell it's Albert that told John not to put himself out, John that answered that it wasn't a big deal, and so forth without having to have it spelled out for us, which can get tedious.