Time for action – finding data and breakout of the while loop
We're going to do something a little different in this loop. Once we find the pony we want, we'll breakout of the while loop. This is handy when looping through a large list of objects. When the desired data is found, there's no sense in continuing to loop through the rest of the list:
Modify
LearningScript
as shown in the next screenshot.Save the file.
In Unity, click on Play.

What just happened?
If we have been searching for Fluttershy
instead of Rainbow Dash
, and not included the break
keyword on line 19, the output would have been exactly the same as the for
loop example. In fact, the break
keyword could have also have been used to breakout of the for loop.

I will skip explaining lines of code that are identical in the for
loop example.
The analysis of the code is as follows:
The code on line 11 with its description is as follows:
int i = 0;
The initializer is declared and assigned the value of 1.
The code on line 12 with its description...