Charles Duhigg in his book The Power of Habit: Why We Do What We Do in Life and Business explains the habit loop.
The habit loop consists of Cue , Routine and Reward.
Cue can be external or internal. A sudden craving for a piece of cake is an internal cue. External cue is when facebook send you a notification that someone mentioned you.
Routine is the action you take from the cue. Like going to a bakery , ordering a cake and having it.
Reward is the pleasure of having the cake. The cause why brain decides to make you want it in the future.
Breaking the habit loop is simple. All you have to do is change the routine to give you a similar reward. When you have the craving for a piece of cake. Have an apple instead. Sometimes you will have to experiment with different rewards to break the loop. This way you change your bad habit into a good one.
Try making small changes to begin with. If you have the habit of checking your phone when you wake up. Change it by reading a chapter.
Do you know that the same loop is used by Facebook, Pinterest etc to hook you with their products? It’s well explained in the book Hooked: How to Build Habit-Forming Products.