Cats’ habits are mainly determined by their instincts. In the early days, cats were tree climbers and hunters. Their bodies are specially designed for jumping or climbing and the sharp claws make it easy.
You have experienced a certain ritual of cats; first, they rub the head, then the flank and finally the tail. These are all gestures of affection from your cat.