Rav Avigdor Miller Z"L said the following:
“Barasi yetzer ha’rah, barasi Torah tavlin lo.” You learn Torah because only the Torah teaches you how to deal with the Yetzer Ha’rah.
Now there are two ways that this works. One way is bi’derech segulah. Merely learning – learning alone – is vitamins against the Yetzer Ha’rah. Hakodosh Boruch Hu sees that you’re learning and He says, “I’m going to protect you from the Yetzer Ha’rah.” Without any other logical explanation – it’s called “segulah.”
And the second way is “sechel.” Because the Torah is full of ideas and ideals that give you the right kinds of thoughts. When a person has no good thoughts in his mind – when his mind is vacant – a vacuum will not remain a vacuum forever and wrong ideas and bad thoughts will come into his head from the outside world. If you fill your mind with good ideas – with Torah ideas – and you’re thinking all the time about these right ideas – in the street, at home – then you won’t have time for or interest in the Yetzer Ha’rah. You walk in the street and think about Ma’amad Har Sinai. You sit at the bus stop waiting and you’re thinking about Yetzias Mitzraim. It takes work to learn how to do that but there is no option. You must fill your mind with good ideas. And then when the Yetzer Ha’rah comes knocking on your door, you’ll tell him, “No, I’m sorry. This place is occupied already.”
------------------------
Any manner of Torah study provides us with spiritual fuel to fight the yetzer hara. Studying Torah automatically strengthens us and makes us want to serve Hashem properly. But there are also specific benefits that come from certain kinds of Torah study.
When we study halachos in depth, Hashem minimizes the yetzer hara in that area. Thus, learning the halachos of Shmirat Einayim is quite helpful. Studying the halachos of a sin also impresses upon us that it really is forbidden, and creates a commitment not to do it. It no longer feels like theoretical knowledge that this is something we shouldn’t do. It feels real. This gives us the strength to stand up to our yetzer hara and confidently tell him no.
When we learn the laws of Shmirat Einayim we may think, "This is impossible! There's no way I can be this careful." But as we study, we understand the wisdom of these laws and the more motivated we become.
The Four Primary Laws of Looking at and Thinking about WomenBelow is a summary of relevant rulings. Familiarize yourself with these laws, and you will have a ready and practical guide to live by: (Shulchan Aruch Even HaEzer, Chapter 21, Law 1)
(1) It is forbidden to look at a woman if she is dressed immodestly. (There are different laws regarding one's wife.)
(2) a. If you know of an area where immodestly dressed women are present, it is forbidden to enter this area. An alternative route must be taken.
b. If there is no alternative route, or the alternative route is impractical or costly, you are permitted to pass through the area providing you make every reasonable effort to avoid seeing the immodest sights.
(3) It is forbidden to have thoughts of desire for any woman. (There are different laws regarding one's wife.)
(4) It is forbidden to derive pleasure from looking at the beauty of a woman, even if she is modestly dressed. (There are different laws regarding one's wife.)