It's just an etiquette. To Dof your cap (lifting your hat when introduced to someone) was common in most of Europe from centuries past.
Also wearing a hat indoors in a persons house was considered bad manners as it meant that you intended to leave immediately after you had just been invited in, more common when most everyone wore a hat outdoors.
I take mine off most places when I first enter a building. Churches, courts, etc. However I won't always do so, such as if I'm shopping and can't be bothered to hold it. It is a sign of respect and proper etiquette to remove ones hat whilst inside a building.
i have no idea O_o!
Also wearing a hat indoors in a persons house was considered bad manners as it meant that you intended to leave immediately after you had just been invited in, more common when most everyone wore a hat outdoors.