Learn Create Your Own Split Screen

Next Page

Like a number of the language features we're learning in this topic, there are other ways of achieving the same result. Break is just more convenient.

For example:

for (var i=0; i < aliens.length; i++) { aliens[i].do_something(); if (game_over) break; }
Is the same as:
for (var i=0; i < aliens.length && !game_over; i++) { aliens[i].do_something(); }
It could well be there's a bunch of circumstances under which you want the for-loop to finish. Handling them via break would be a lot clearer (and less error-prone) than bundling them all into the termination condition of the for-loop.

 


 

(In the example, recall that "&&" is like a Scratch "and" block, and "!" is like a Scratch "not" block)