Breaking the loop
Loops can also be designed to stop themselves from executing any further. To do this, we use the break
keyword. Whenever Unity executes a line containing the break
keyword, it will stop the loop it is in and continue executing from the line just after the loop block.
We use break
mainly for performance reasons. In the preceding example, we are looking for the "Adam"
string. Once we've found it, there is no reason to iterate through the remaining elements of the loop. So, line 24 breaks the loop and the execution jumps to just after the loop block—line 27.