"When" can simply mean at the time that something happened. So it is basically equivalent to "while" in the swimming examples. ("While" emphasizes that the activity lasts a period of time.)
When walking home late at night, be careful.
Yes, since there is no specific situation/time referenced here, this would normally receive a habitual reading (such that "when" can be paraphrased as "whenever"). Though perhaps less common, a "while" clause can receive a habitual interpretation as well:
- While conducting a job interview, it is important to convey a positive attitude.
For me, using "while" rather than "when" here focuses more on the stretch of time that the interview occupies. But either is acceptable.