I think there are some people who can sense their surroundings and flip a switch. There are others who simply can't.
I work in an office. If I started swearing regularly I'd probably find myself out of a job. I'm guessing that's similar for most people, who can control themselves given the surroundings.
Occasionally I will find myself watching a game on my TV saying something I wouldn't want my kid to hear. However, if I'm at a game in person or watching at a public place, there's almost no chance you'd hear the same utterances. I'm guessing a lot of people can flip a switch when in public, but choose not to when they don't believe they'll get in trouble for it.