- Another way that program flow can be altered
is when one or more statements are repeated over and over.
This process is referred to as a loop.
The C language offers several ways of looping.
In this movie, I present the 'while' type of loop.
You'll learn how to set up the loop,
determine when to stop looping,
and how to break out of a loop before it's done.
A loop is a repetitive way to control program execution.
You must specify a condition that makes the loop repeat,
a set of statements that repeat,
and a way for the loop to stop.
In this code, a 'while' loop counts from one to 10.
The looping condition is specified on line eight.
It reads that, while the value of variable x
is less than or equal to 10,
as long as that condition is true,
the statements in the loop between the curly brackets
at line nine and 12 will repeat.
The value of x is initialized at line seven.
At line 11, the value of variable x is incremented.
So, as long as x keeps inching up, the loop repeats
until x is greater than 10.
Build and run this code.
And you see the values from one through 10 displayed.
Now say you wanted to count to 20.
Can you, just off the top of your head,
imagine where you would make that change?
It's simple.
Change the condition in the 'while' loop
to 20 instead of 10.
Save that change.
Build and run.
And now, values one through 20 are displayed.
Now, suppose instead of going by one,
you wanted to display only the even numbers from two to 20.
This requires a little bit more thought
because it changes two things,
the initialization
and how the value of variable x increases.
The first change is
to initialize variable x to two at line seven.
You're going to be counting by twos, start at two.
Then, modify line 11 so that the value of x
is incremented by two,
which is not really an increment,
it's a mathematical function.
So x equals the value of x plus two.
Save.
Build and run.
And there you see even numbers between two and 20.
Finally, it's possible for loops to count backwards.
If you were going to count backwards,
say, from 10 to one,
can you think of how you would modify the code
so that that would happen?
Obviously, there's a few changes
that would need to be made.
If you're going to start at 10,
you need to change the initial value to 10.
Then, the condition needs to change, as well,
because already, the loop wouldn't repeat.
Value of x is less than or equal to 20.
So the condition should be
while x is greater than zero.
Then the statement at line 11 needs to be changed, as well.
The value of x should be decremented
each time the loop spins.
There's the decrement operator.
Save.
Build and run.
And there the loop counts from 10 down to one.
Open exercise file while2.
This code uses the srand and rand functions
introduced in an earlier movie.
These functions generate random numbers.
In this case, a 'while' loop displays 10 random numbers.
Build and run the code.
Impressive.
But let me show you a couple of tricks
to clean up the numbers.
The first is the modulus operator, which is a percent sign.
Modulus calculates the remainder of a value,
like leftovers.
In fact, open up the exercise file whilemod
to see a demonstration.
Here, I'm using a C program to teach you C.
A 'while' loop marches through the values seven through 30.
It shows you how the modulus operator affects
each of those values.
Build and run the code.
The modulus operator gives you a remainder.
So, for example, 10 % 7 yields three.
That's because seven goes into ten once,
but with three remainders.
And you can see how the value is always going to be
between zero and six for % 7,
no matter what initial value is used.
Return back to the while2 source code.
Edit line 15 so that the printf function
displays the value of r % 100.
This change limits the output to values between zero and 99.
Save.
Build and run.
You may see some single digits in there.
Let me run it again to get some single digits.
There they are.
The single digits don't really look properly formatted.
To line up the numbers,
you can edit the printf statement again.
This time, insert a two in the %d placeholder.
The two sets the width of integer output
to two characters wide, right justified.
Save.
Build and run.
Here you see a single digit, but it's lined up on the right,
which looks more pleasing.
Another type of loop is the 'do while' loop.
It's kind of an upside down 'while' loop.
But is has the guarantee that the loop
always executes at least once.
Open exercise file dowhile.
The 'do while' loop starts at line nine
with a do statement.
It has no condition.
Instead, its statements are executed one after the other.
The while condition appears at line 14.
The loop relies on variable ch as its condition.
That variable is initialized in line seven
and then manipulated in line 12.
Build and run.
The z isn't printed because it's an exit condition.
If you want to see the z,
you need to save the exit condition to z+1.
Save.
Build and run.
Open exercise file whileendless.
This code demonstrates an endless loop.
There is no exit condition
because the 'while' loop's evaluation is always one.
Build and run the code.
Press ctr + c to cancel the program
and stop the madness.
Now, endless loops do have their place,
but most of them are unintentional.
You can, however, fix the loop by using the break statement.
Insert line eight, which simply says 'break,'
and then a semicolon.
Save.
Build and run.
The loop spins only once because the break statement
busts out of it.
You can even use the break
to bust out of a loop prematurely.
Load exercise file whileendlessbreak.
The 'while' loop's condition is infinite
and it cannot change.
But variable a is used to monitor the loop's iterations.
And once a is greater than 10, the loop is busted.
Build and run the code.
Of course, a better solution would be
to evaluate variable a at the 'while' loop's parentheses.
That's something I hope you try on your own.