The brain often does create looping conditions. When these loops involve a large number of neurons, we call it a seizure. An analogy in electrical engineering would be a feedback loop -- such as literal feedback between a microphone and speakers. In the brain, fortunately, these loops are usually self-dissolving since the neurons involved will often run out of neurotransmitter or energy, or inhibitory neurons will get involved (like referees breaking up a fight). Many of the neurotransmitters and hormones in the body act as regulators of ongoing processes. There are almost always negative feedback mechanisms whose purpose is to prevent overload on various systems. The equivalent in programming would be using a counter variable in a while loop to break if it loops too many times.
This all, of course, was with respect to short pathways (small number of links). Larger, more complex pathways such as thoughts and goals are probably handled in part by different mechanisms, such as the instinct of not wanting to waste too much time getting nowhere. At the same time, getting frustrated and rage quitting is a perfect example of hormones and neurotransmitters rising to the point of stopping the loop of trying to do something that is not working as desired.