Fasting helps us to achieve Taqwa by training us to be conscious of Allah.
When we are alone making Wudu and we put water in our mouths, we can easily swallow some of it and no other human being will know, but what is it that prevents us from doing so? It's our realization that even if nobody knows about it, Allah still knows and our fast would not be accepted. Our consciousness of Allah prevented us from swallowing the water. Thus if we are alone thinking about committing a sin, we should be in the same state of consciousness. If we fast often, we will be training ourselves to be conscious of Allah at all times.
Fasting is not just about starving our bodies and holding back natural desires. it's about helping us achieve Taqwa.
"O you who have believe, decreed upon you is fasting as it was decreed upon those before you that you may attain Taqwa." al-Baqarah / 183
The Messenger of Allaah (صلى الله عليه وسلم) said: “Whoever does not abstain from false speech and acting upon it, Allaah has no need of his abstaining from his food and drink.” Narrated by al-Bukhaari
If fasting does not prevent us from committing sins, then we have not fulfilled the purpose of fasting.