- [Narrator] Loops in programing
are a way of repeating actions.
Let's look at three different kinds of loop.
A ForLoop, a WhileLoop, and a DoWhileLoop.
All of the loop examples are attached to a GameObject
and will check the response by looking at the console.
First off, let's take a look at a WhileLoop.
The WhileLoop is used to simply perform an action
while a condition is met.
In this example, we have a variable called "cups in the sink."
We have four cups in the sink.
While the number of cups in the sink is more than zero,
we need to wash them.
So, we're simply looking for "I've washed a cup"
and subtracting one from the value of
our "cups in the sink" variable.
So, this loop will continue four times because we have
four cups.
Let's see it in action.
And, you can see, we've washed the four cups.
Next, let's take a look at the DoWhileLoop.
The DoWhileLoop functions almost identically to the
WhileLoop, with one major difference.
WhileLoops test the condition before the loop body.
However, DoWhileLoops test the condition at
the end of the body.
This difference means that the body of the DoWhileLoop
is guaranteed to run at least once.
Here is the DoWhile script.
We can see that we have a boolian variable called
shouldContinue.
This variable is set to false.
Next, we have the DoWhileLoop.
Notice the syntax for the DoWhileLoop.
We start with the key word "do" followed by open and closed
braces.
Whatever code is between these braces makes up the body
of the loop.
After the body, is the "while" keyword
followed by the conditional.
In this case, the loop would only continue while
the variable "should continue' is equal to true.
We can see that the variable is set to false
so this conditional will resolve to false and the loop
will exit.
Finally, notice the semicolon after the conditional.
WhileLoops don't have the semicolon, but DoWhileLoops do.
Back in our scene, we will enable the DoWhile script
and run it.
We can see that the phrase "hello world" is printed
to the console.
Even though our condition was set to resolve to false
in the beginning, the body of the DoWhileLoop always
runs at least once.
Next, the ForLoop.
This is probably the most common and flexible loop.
The ForLoop works by creating a loop with a controllable
number of iterations.
Functionally, it begins by checking conditions in the loop
seen here as i < numbEnemies.
And, if the condition is met, it carries out
the instructions in the loop's body
meaning, what is between the curly braces.
After each loop, known as an iteration, it can optionally
increment a value.
Seen here, in the third argument, where i is being
incremented by one each time.
This process then starts over.
Let's look at this from a coding standpoint.
The syntax for this has three arguments.
First off, we introduce a variable that is known as the
iterator.
This is used to count through the iterations of the loop,
meaning the loops themselves.
The second argument is the condition that must be true
in order for the loop to continue.
Finally, the third argument defines what happens to the
iterator in each loop.
Usually, this means adding to the iterator in order
to step through the loop.
It's common in programming to start counting at zero.
So, for the first loop, i will be zero, and then
the operation I plus plus adding one to i will be performed.
Then, on the second loop, i will be one, and so on.
The loop will continue until i is no longer less than the
numEnemies variable.
So, we have an integer called numEnemies,
which is set to three.
Once this loop has happened three time, i will be
equal to the value of numEnemies, and therefore the
loop will not be run again, as the validation condition
will be false.
We will not see the message creating enemy number three
in the console, because when i is equal to three, it will
no longer be less than numEnemies, and so it will not
run the commands in the body of the loop.
In this example, we're logging the value of i to the console
alongside the text "creating enemy number."
We're using this to demonstrate iterating through a loop
along with a potential practical application of this
technique.
Put simply, any operation that you need to carry out a
defined number of times can be placed within a ForLoop.
Let's see it in action.
And, you can see, it logs to the console three times.
First, showing the value of i is zero, then one, and then
two.
There's a lot of flexibility in what you can do with
different loops, and you should always consider using them
when repeating different actions based
on certain conditions.