There is much discussion, especially among the Moroccan Poskim, as to whether covering one’s head is a Halacha, and that by not doing so one violates a prohibition, or if covering one’s head is simply a Midat Hasidut, a pious act beyond the letter of the law. The Shulhan Aruch (Orah Haim 2:6) writes that one should not walk a distance of four Amot (roughly 6 feet) or more with one’s head uncovered out of honor for Hashem’s Presence. The language of the Shulhan Aruch seems to imply that covering one’s head is a worthy act and that one should try not to go bare-headed, but does not go so far as to say that walking with a bare head is prohibited.
Indeed, the HIDA (Birke Yosef, § 2), based on the Zohar (Pinhas) understands the Shulhan Aruch to mean that it is a pious act and is only obligatory for a Talmid Hacham, a very learned person. Furthermore, Rabbi Ben Zion Abba Shaul (Or LeZion vol. II, ch. 7, § 13) and Rabbi Yosef Messas (Mayim Haim, Orah Haim, § 23) write that there is no obligation to cover one’s head when walking more than four Amot.
Nevertheless, Rabbi Shalom Messas (Tevuot Shamesh, Orah Haim, § 32) cites different sources from the Shulhan Aruch which indicate that covering one’s head is not a pious act, but rather a Halacha. He continues by saying that if one were to sit or remain in one spot, one would not be required to wear a Kipa. Rabbi Shlomo HaKohen of Debdou (Lecha Shlomo, Orah Haim, § 7) also writes that covering one’s head is an obligation. It should be noted that if one were to recite a blessing or a prayer, even sitting in one spot, all authorities agree that one should cover one’s head.
The above discussion refers to situations in which wearing a Kipa would be considered normative. However, in situations where it is not considered the norm, one can be lenient and not wear a Kipa. For example, if one goes swimming, one is not obligated to wear a Kipa as long as one is at the beach or by the pool since it is not the norm to cover one’s head in such an instance. Also, in light of the disagreement whether a Kipa is obligatory or not, there is a spectrum of opinions as to how far one has to go to use an alternate head covering, like a cap, in locales where wearing Kipa could be dangerous or otherwise unacceptable.
Summary: There’s a difference of opinion regarding the obligation to cover one’s head when walking beyond four Amot. One must wear a Kipawhen praying or reciting a blessing. One may be lenient when one is sitting or when one is in a place where wearing a head-covering is not the norm.