5 - Loops
Sometimes when you are writing code, you will want to perform a set of statements more than once. That is where looping statements come in. Looping is a very common technique used in programming, and there are a variety of methods that you can use to do it. I will go through some of the loops, and show you basic examples of each in this tutorial.
A while statement is executed while the statement within is true. While loops are pre test loops meaning that it is possible during an execution of code, the condition will never be true and the code within will not be run. A common use of a while loop is creating an infinite loop by using putting while(1) as the while loop. Using while loops in Unity can be rather tricky at times, and if you use them incorrectly you will crash Unity. Make sure that you do not create infinite loops, and also keep them out of the Update function (already a looping statement).
Do statements are similar to while statements, except that they are post test loops. This means that the loop will execute at least once, but will continue to execute if the while statement at the end is true. These are useful if you want a set of code to run at least once, but also have the option to run multiple times. Like while statements, be careful how you use do statements. Don't create infinite loops, and keep them out of updating methods.
Notice how even though the while statement is false, the code within the loop is still execute once.
For statements are looping statements which let you control how many times they are executed. There are three parts to a for statement: The init expression, conditional expression, and loop expression.
The init expression creates a variable and initializes it to some value that you define. Probably 90 percent of your for loops will start with the init expression being initialized to zero, but there are other use cases in which you would use a different value. In the example above, the init expression is int i=0;.
The conditional expression checks the value of the variable define in the init expression, and determines if the loop should continue to run. In the example above, the conditional expression is i<10;
The final part, the loop expression, is used to increment the value defined in the init expression. A common example is to increment the value by one each loop using the ++ operator. Other options would be to increment it by 2 so that you can get even or odd expressions, or using the -- operator. Pretty much any kind of operation can be used in the loop expression. Just note that if your loop expression causes your conditional expression to never be met, your for loop will be infinite. In the example above, the loop expression is i++.
One last note on for statements. An infinite loop can be created by omitting the three expressions in the for statement.
Foreach statements are useful for iterating through a collection. They are similar to for statements, but do not require you to enter the size.
There are a variety of expressions that can be used to control the flow of code through a looping statement. The keywords break, continue, goto, return, and throw can all be used. I will cover these statements in another tutorial, but I want you to be aware that there are additional ways to control the flow of code in loops.