To break out of a loop in a script you simply use the "break", "return" or "halt" commands. Each one does a different thing, I suggest looking them up in the /help
That's not really how you solve broken loops though. An infinite loop means you didn't specify an end condition.
Read
http://www.kthx.net/ftb to find out where the bug in your code is.