- Home /
How to stop a for loop before it completes it's full cycle?
Hello, I am creating a for loop, that I wish to stop once a given statement is found true. How would I go about doing this?
example:
for(var i in objectArray){
if(i.transform.position.y <= 0){
doSomething = true; stop the loop;
}
else{doSomething = false;}
}
This above code is simili code, just to give an example. In my code, I have many if statements that can override each other, thus I need to stop the loop once a statement is validated.
Answer by Chad 2 · Dec 31, 2010 at 09:17 PM
Use break
.
for (var i in objectArray) {
// do stuff
break;
// this would do more stuff it a break wasn't there.
}
Note: You made not need the ;
on the end of break
; this is untested code, and I don't remember if it is necessary.
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
JS Loop question 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
[SOLVED]Does this loop actually end? 1 Answer