We've already looked at the While Loop
but what we're going to look at now is a different form of
looping structure called the For Loop.
Now a distinct feature about the for loop
is that it allows you to specify and control
exactly how often a loop should run
depending on what kind of
sensors and inputs you're actually using
to make this work you have to give it three
particular forms of information.
The first thing is you have to give it
some kind of counter or variable
that is set up at the start, thats going to keep track of
how often the loop has run.
In any kind of loop you need some kind of test
thats going to allow you to decide
whether or not the loop should end
and the third thing we have to do, again
as you have already found with the while loop
is you've to have some mechanism that changes the value
of the counter that you're using
otherwise you'll never exit the loop
lets look at how that works.
To make this test work
you can use logic expressions
again these have been looked at before,
lets just reiterate then for a moment
so we've got a greater than statement
which test whether one side is greater than the other
and its partner less than
we can also test if something is
greater than or equal a particular value
or less than or equal to a particular value
then we've got the particular test
of being equals and again
the actual logical test for if one side of the test
is equal to the other is to use the double equal sign
not the single one, equally important
is the not equals sign
these are the different ways that we can check
whether one side
matches or doesn't match another and then whether
the loop is true to continue.
Here's the general layout of the for statement
as you'd expect with any loop
its got a marker at the start the brace
to see where the code that will be executed in
the loop is and a marker at the end
to see where the loop should end
the key differences that you have
are that all the control
is done inside this bracket after the for statement
we specify what's the starting condition
what check are we going to perform
and then how were going to change
the starting value everytime the loop runs
also its worth noting there's no semi-colon
at the end of the statement
because its not actually the end of a statement
the statement finishes down at the bottom of these braces
which act in the same way a semi-colon would.
Here's a small example
we've already looked at this for the while statement
so what we've got here, is we have to setup
a variable first of all, which we're going to use
and then just as the previous slide shows
we setup a starting condition
that sets it to be zero at the start, we setup
the condition to keep it in the loop so long as the value of i
is less than five this loop will continue to run
and then finally we have how i changes
in this case we're using the standard increment command
thats built in to the programming language
and then we've got the actual operation
between these two braces
and as you can see this particular case
is going to drive forward for half a second
and do it five times
so this loop will run five times
for when the value of i is zero
then when i is one, when i is two
when i is three, when i is four
and then once it goes through the loop again
and increments to i being five
and exits the loop and continues on executing the code
beyond the for loop
and this just highlights the roles of these elements