In this lesson, we're going to talk about loops.
Loops are one type of flow control
that allows you to be able to control
which order the code is going to be executed
in a given program.
For example, if I wanted to run a piece of code
10 different times,
I can either copy and paste that code 10 times in a row,
or I can use a loop and have this logic take over
that will allow it to continue doing that function
until it's passed through 10 times.
Now, when it comes to programming,
there are three basic types of loops that you can use.
There is a for loop, a while loop, and a do loop.
Now, the for loop is the most simple type
that you're going to use.
For loops are used when you know exactly how many times
you want to repeat a certain block of code.
For example, I just gave you the example
of repeating a piece of code 10 times.
So you could set up some code
that will go through that loop 10 times and then stop
because you already know you want it to happen 10 times.
To do this,
you'll simply write For i = 1 to 10 OUTPUT(i) Endfor.
This means we're going to start with one
and go through the loop,
and each time we're going to output
or print to the screen the value of i.
So what you're going to see
is that on your screen,
it's going to go 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
and simply count up until it hits 10,
and then it will stop executing that loop.
Now, another kind of loop we have
is what's called a while loop.
With a while loop,
we don't know exactly how many times
we want to run through the code.
Instead, we want the code to go until something happens.
Now, what we're going to do is set up a condition,
and we're going to test for that condition
at the beginning of the loop.
For example, if I have the code, i = 0,
and then, While i < 10 OUTPUT(i) i = i + 1 Endwhile.
This will perform that while loop
anytime the value of i is less than 10.
In the case of this code,
we initially set the value of i at 0.
0 is less than 10, so we'll go through this loop.
We're going to display 0 to the screen,
and then the value of I will equal 0 + 1, or 1.
So we now have i = 1.
We test the condition again.
Is 1 less than 10?
Yes it is.
So i'll output 1 to the screen.
We'll then add 1 + 1, giving us 2.
And we'll continue to do this every time
until that value of i is now set to 10.
So what would be the last number on the screen?
Well, it's going to be 9
because we're going to go through this loop
from 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
Once we've outputted 9 to the screen,
we now have i = 9 + 1.
That means i is going to equal 10.
When we go to check the condition again at the top,
we see that 10 is not less than 10,
and therefore we are going to end that while loop.
The third type of loop we have is known as a do loop.
Now, do loops are used
whenever you have an indefinite iteration
that you want to happen.
This means we're going to do this thing
until some condition is met,
but the difference is we're testing the condition
at the end of the loop instead of the beginning of the loop.
Now, why would we want to do this?
Well, we would want to do this
if we always want to make sure that code runs at least once.
For example, if I had the code Do OUTPUT(i) i = i + 1
Until i > 10, I can then make sure
that I'm always going to go through this loop at least once
no matter what the value of I already was.
For example, if I set i = 50 right before the Do statement,
I would still go and output 50 to the screen,
then i becomes 51, 50 + 1.
Now, I'd check it and say, is i greater than 10?
Yes, 51 is greater than 10.
I'm going to stop and only have that one number
printed to the screen.
Conversely, if I set i = 0,
it's going to look a lot like the while loop
that I already did.
I'm going to help put 0 to the screen.
Then I'm going to have 0 + 1 = 1.
Is 1 greater than 10?
No.
So I'm going to go and do that again.
And I'll put 1 to the screen.
And I'm going to continue to do that
until I output 10 to the screen
because 10 is still not greater than 10,
it's equal to 10 but not greater than 10.
So I'm going to go through that loop
with output of i being output of 10,
and then getting 10 + 1 being 11.
And now when I check it,
I'm going to stop because 11 is greater than 10.
Now, really, these are some really simple examples,
but I just wanted to show you how these for, while,
and do loops can be used.
Now, in the real world,
you're going to find much better uses for these three tools.
For example, when I use a do loop,
often I'll do it when I'm reading a file.
This way I can do something like Do OUTPUT,
the file as I'm reading each line,
until the file is at the end of the file.
Once I reach the end of the file,
I can stop because I don't want to read anymore.
That's a great use case for doing a do loop
instead of using something like a for or a while.
Now, as we move into some of our examples
for each of the different programming languages,
you're going to see the different for, while, and do loops
that are being used in a real-world context,
and this will help you better understand
when to use each one during your penetration tests.