- In this video we are going to learn a new
structured command sequence in C called loops.
Loops are very useful when we need to repeat
a block of code more than once.
For example, on different data sets,
same data set but with different parameters,
or we need to repeat execution
until some condition is met.
Can you think of an additional instance?
Let's examine one such construct in C
called the For loop.
There are many types of loops in C,
this is just one of them.
The For loop is very helpful when you know
a precise number of time a set of commands
or function calls must be executed.
The syntax for For is quite simple.
You have to give it three parameters.
One is a start value, the max value,
when to stop, and the increment.
For example, if you would like to execute
something 10 times, the start value could be one,
the max value could be 11,
and the increment would be one.
Then the k, which is the counter in the loop,
will perform this from one to 10.
The way this works is k is initially assigned
a start value, for example, one, it checks
whether that value is less than the maximum value,
in our case it would have been 10,
so it hasn't reached the maximum yet.
So we are ready to perform doSomething
and doAnotherThing, then k is being incremented,
and again it is checked against the max value.
Since now k equals two, which is still
less than 10, the loop continues
and k equals three and so on.
Once k equals 10, it's the last time
it will be executed if we set
the max value to 11.
Next time k is 11.
It is no longer less than 11, and therefore
the loop stops and the rest
of the program continues.
Let's see how this is implemented in C.
For this particular exercise, I set up
a very simple example to print a list
of all the even and all the odd numbers
between zero and 100.
Let's take a look at the function that lists
even numbers on line 25.
I'm just printing a title first,
and then in line 28 I am declaring an index,
or a counter, for a loop, which is then
being used in line 29.
In my case, the start value is zero,
the max value is 100, and the increment is two
because I want to start from zero
and then print two, four, six, eight, and so on
until the last one is less or equal to 100,
in which case I will stop.
Let's see how this works.
At first i equals zero,
and on line 30 I'm printing i.
I am then testing whether i is less than 100,
which it is.
I'm incrementing i by two, so now i equals two,
and I perform the operation again.
Since two is less than 100, I continue.
I increment i to four.
I check against 100.
It's still less than 100, and so on.
In a similar manner, I'm using the function
listOddNumbers, which is identical
to listEvenNumbers with one difference-
i starts at one instead of zero because one
is the first odd number.
If I start at one and keep adding two,
I'm going to print eventually all the odd numbers
between zero and 100.
Notice one another small difference.
In line 28 I am declaring i outside the For loop.
In line 37 I am declaring i right where I need it.
It is perfectly fine in C to declare a variable
and use it right away.
This is not just used for For, but is being used
in other instances as well.
Either way, it is up to you which option you prefer.
Let's run this program and confirm that
it indeed works the way we planned.
We have to scroll back to the beginning.
We can see already all even numbers
between zero and 100,
zero, two, four, and so on,
and the last one is 100,
and then all the odd numbers,
one, three, five, and so on all the way to 99
because 100 is the upper boundary
and 100 is an even number.