Hello, and welcome back.
Still going to talk about a little bit about control flow.
So let's dive right into loops repeating code.
First, we're going to talk about this for loop.
The for loop is
going to be all about repeating code. So just as a reminder,
repeating code is a way to
allow for not duplication and you're actually maybe executing more than one like code
at a time.
So the c programming language has certain constructs
designed for the language that you can use when you want to repeat code.
And this allows you to repeat a block of statements until some condition is met.
So it's similar to the for loop and that you're going to evaluate a Boolean expression,
a condition. You can't run forever.
When you run in a loop forever, that means it's an infinite loop.
And there are only certain times you may want to do that
usually again for like monitoring or listening to something.
So the number of times that a loop is repeated can be controlled simply by account.
There are 2 types of loops
that you can do, a counter control loop or like a sentinel control loop is what they call it.
A counter control loop means that the loop runs
10 times, 20 times, 30 times, a specific amount of time.
It repeats the same statement block inside the loop
a number of times.
You can have another type of loop where it's not counter controlled.
It may run 5 times, it may run a 100, it may want 20,
it all depends on a condition, a variable or something. So for example,
you can run a loop that says I'm going to run my loop over and over and over
until the user enters quit.
So until the variable changes to quit. It's not counter controlled. It's not dependent.
Again, it may run 10 times, it may run 100 times.
These are the 2 different type of loops. And so I’ll provide examples of both,
but just think of this at a high level when would I want to repeat code?
Well, I may want to repeat code 5 times, a certain amount of time,
definitive amount of time or I may want to put code
based on a certain condition entered by a user, and that's called sentinel.
Our for loop has the for keyword. It's used to execute
a given number of times.It's usually used for counter control, but it doesn't need to be.
You can use it for counter control loop or a sentinel to control loop.
It's up to you. But a lot of the examples in a lot of the practice
is counter-controlled, a certain amount of times.
If you want to display the numbers from 1 to 10, for example, you would use the for loop.
You would not want to write 10 lines of code
with the same printf statement in there displaying a different number.
That's very inefficient. It's very time consuming.
You would use a for loop to repeat code.
What you would do is you would do something like this.
Here's an introduction to what the for loop looks like,
the syntax.
You have the for keyword, and then you have these parentheses.
Again, the parentheses identify different things that are going on
for the for loop.
Then you have a bracket, and you have statements inside the brackets.
So again, the brackets represent the body of the loop,
just like the brackets representing the body of an if statement.
If you have one single statement in there, you don't need the brackets,
but good practice to always use them.
So in this example here, you see the for key word, that should be the first thing you identify.
The operation, how long it's going to run is controlled and what appears between the parentheses
after the for keyword, which we're going to go into specific detail.
There's basically 3 different expressions inside there.
And you can recognize them because they're separated by the semicolon.
So the semicolon says this is an expression statement.
So what are those 3 things doing, we're going to describe.
The action that you want to repeat
is inside the body of the loop,that's what happens so many times.
In this example, the body of the loop is printf.
So if a condition is evaluated to true, it will keep printing that number.
And again, for single statements you can omit the braces.
So let's go through this in detail.
the general pattern for the for loop is you have this type of statement.
You have a for and then you have a starting condition, this is pseudocode.
I’m trying to describe the for loop here.
So the starting condition is the first thing, everything before the first semicolon.
The second thing is the continuation condition
with a semicolon at the end and then the last thing in the between the parentheses is action per iteration.
And I want to describe each one of these. And then after that inside the body is the loop statement.
The loop statement is pretty easy. That's just inside the body.
That's the code that's going to be executed that's repeated each time.
That's pretty basic.
And again, you can either have break it braces if you have multiple statements or
omit the braces if you have a single statement.
The starting condition, that first thing inside the parentheses
before the first semicolon is usually but not always
it usually initializes a variable, called the loop control variable.
That's the that's the variable that you're going to start at looking at each iteration.
So the loop control variable is typically a counter of sometime,
not always again, I want to emphasize that,
it doesn't have to be. But a lot of times in a for loop it's a counter,
and it tracks how often the loop has been repeated.
That's what this starting condition and the loop control variable is doing.
You can also declare and initialize several variables of the same type here
with the declaration separated by commas. So you can have multiple initializations here
variables.
Now depending on what c compiler you use,
you may not be able to declare those variables inside the for statement.
If you use anything below the C99 compiler,
you have to declare the variables outside of the for statement and then you would just initialize them
in this where the starting condition says,
but we'll go through some of those examples.
But the starting condition is where you initialize variables.
It's the loop control variable and it's named for that reason because it controls
how often the loop is going to execute when you initialize it.
The next thing I want to talk about is that continuing condition, continuation condition,
the part that's before the after this first semicolon in the for loop.
This is a logical expression.
This is the condition that's going to evaluate to true or false.
This is when you're going to jump in the loop or exit the loop.
This determines whether the loop should continue to be executed.
As long as this condition has a value of true, the loop is continued to execute,
all the statements in the body are going to get executed.
This will typically check the value of the loop control variable.
So you initialize the loop control variable in the starting condition
and then you look at in the continuation condition.
You can put any logic or any arithmetic expression here as long as you know what you're doing.
The continuation condition is tested at the beginning of the loop rather than at the end.
This is referred to as a pre-test loop.
You'll notice the difference when we talk about the do-while loop, which is a post-test loop.
But what this means is that the loop statement may be executed once,
it may be executed 0 times.
It all depends on whether or not that continuation condition evaluates to true or false.
If it evaluates to false, you're never going to execute the body.
If it evaluates to true, you'll execute that body at least once.
The action per iteration which is at the very end of the for loop after the second semicolon,
that usually does some kind of modification of the loop control variable.
It will increment or decrement it if it's a loop counter
or if it's a sentinel loop, it may ask the user to enter more input.
You can also do more than one modification of a variable.
You can modify the loop control variable, and you can modify other variables.
You just have to separate them by commas.
But to summarize everything I said,
we have this little table that describes that.
But before we talk about that, let's look at a little example.
So here's an example. We have the for keyword.
And then you'll notice that we have int I equals 1 j equals 2,
that's where we're initializing variables.
And we're actually initializing 2 variables here separated by the comma,
typically in a for loop usually just initialize one, the loop control variable.
But in this case, we're initializing I and j.
And then after that semicolon, we have the condition.
When are we going to enter and whenever we're going to exit this loop.
We're going to enter as long as I is less than equal to 5.
That's our Boolean expression evaluates to true or false.
And then after that second semicolon,
we're going to modify that loop control variable or any other variables we've concerned it.
Here we're doing a plus, plus I, this is an incrementation operator,
prefix I. And then we're assigning j equal to j plus 2.
That's going to be usually your x condition,
modifying that loop control variable.
And then the body of the loop just has a single statement, so we don't need the brackets.
We're just printing out the values of I times j.
So the output produced by this code
is going to be the values 2, 8, 18, 32 and 50 on a single line.
The first iteration of the loop, I is going to be equal to 1, j is going to be equal to 2.
And so it's going to say I times j, which is 1 times 2 and it's going to display it.
It's going to check to make sure I is not greater than 5, I is not it's 1.
So it's going to go back up and execute a second time.
And we're also modifying that variable I in our last statement.
So in the second iteration of the loop, I is going to be equal to 2.
And j is going to be equal to 2 plus 2 which is 4.
So now we're going to multiply 2 times 4, which is 8
and we're going to display it and then we're going to check
is I greater than 5? No, I is still 2.
So we modify I to now be equal to 3 for the next iteration,
and we modify j to be equal to
4 plus 2 which is 6.
And then for the third iteration of the loop,
we multiply I times J. So we say 3 times 6 and we display 18.
Before we execute the fourth iteration, we check to see if I is greater than 5.
No, it's 4. Then we then we increment it, we say it's 5. It's equal to 5,so it will still run.
Then we'll do 32, and then we'll do 50.
So this is a for statement. You want to run this and execute
it in the code browser and play around and change things.
But what we're doing here is we're doing 3 separate things on a single line of code.
We're initializing the loop control variable or any other variables.
We're having our Boolean expression after our first semicolon, our exit condition.
And then we're modifying the loop control variable and any other variables we want
as that last part of the for statement.
So to summarize what we're doing I found this nice little handy chart
from a book and I wanted to show you. This summarizes everything.
That first semicolon initializes the loop control variable.
After the first semicolon, we're doing our condition, our Boolean expression.
And then after the second semicolon,
we're modifying that loop control variable. And this is the typical example you see.
This is a counter control loop that starts at 1 and goes all the way to 10.
So this is going to actually execute 10 times.
The key being there less than equal to 10. So you're going to see relational operators as our conditions,
just like we saw relational operators as our conditions on our if statement.
The key here is you want to modify that loop control variable or you're never going to exit.
So the plus plus count is very important.
And as far as pre or post incrementation whether that plus plus is before the variable or
after the variable,
you don't have to worry about it because
it's only going to get incremented or decremented after the iteration of the loop,
so it doesn't matter if you do plus plus count or count plus plus.
And also again depending on the version of c that you're running for your compiler,
you may be able
to declare the variable count inside the for loop or you may have to say int count
equals 1 or in count outside of the for loop and then initialize it in the first semicolon.
C99 added the initialization and declaration
in the same part of the for loop.
But this is basically how for loop works.
Now let's go into a little example, I want to show you the flexibility of the for loop.
Here's an example where we're going to basically just sum some integers.
And in this example, we're going to ask the user enter a number of integers they want to sum.
We're going to store that in a variable called count.
We're now going to have a for loop that goes through all of those
depending on the number they entered iterate through that many times.
So here we say 4, we have unsigned int, doesn't have to be unsigned
for int I equals 1. We're initializing the loop control variable.
And then we're saying if I is less than to count, that's when we're going to exit
and then we're going to modify the loop material plus plus I.
So this is going to run. Again,
the 1 to however the number the user entered, 10 or 20.
And all we're going to do is we're going to say sum equals sum plus 1.
Now the for loop is so powerful because the for loop allows you to
modify many variables.
We don't even need to put that sum plus equal I in the body of the for loop.
We can put it at the end of the last statement.
So in addition to incrementing I by 1, we can just say sum plus equals I plus plus.
So we did everything in a single line of code,
and that shows you the flexibility. it's all because we can modify the variable
in the for statement itself. We don't have to have that line of body.
So let's take a look at this example in code blocks
to show you how you can manipulate it and see what the output is.
Here's our example that we had in the lecture. I want to go ahead and try to compile this.
And when I compile this, you're going to notice that I have an error.
It says I cannot declare my variable inside my for statement. This is because I’m running
an older version of the c compiler. I’m running the C89.
You probably won't have this.
But to solve, if you ever see this type of error,
all you have to declear it outside of the for loop. So you just say unsigned I.
And then inside the for loop, you initialize it,
something like this. Now let's try to build
and I just have to get rid of some of these format specifiers.
So when I compile successfully and I run this program,
you're going to see how the loop works.
It's going to ask me to enter the integers I want to sum. If I enter 5,
it's going to sum up 1 2 3 4 and 5 which is 15.
And what we could do to debug this is we could add a bracket to the for loop,
and we could want to just see how many times it runs, we can add a print statement
not to necessarily debug it, but just see what's going on, we could say inside loop.
And this will tell us that the loop runs 5 times
because it's going to run I to count.
So what I want to demonstrate is how many times this is running. So now when I build it
and I run it, I’m going to see what's happening for each iteration.
So I’m going to I say 8.
Inside loop, inside loop, inside loop, I didn't add a line feed. So it's tough to read,
but inside loop is displayed 8 times.
And then I sum up each of those numbers inside loops, so the answer is 36,
1 plus 2 plus 3 plus 4 plus 5 plus 6 plus 7 plus 8.
So you can play around with this. But again,
we initialize our loop control variable. We check the condition to exit. We're going to exit once we hit
whatever the user entered. If the user entered 8, we're going to run 8 times
and then we have to modify I. If we didn't modify I here, you'd have a forever loop.
And what would happen when you ran the program,
it would run forever.
So you'd see after you entered the number, your program will never exit.
So say we enter 6, I hit enter, look at what's going on,
just printing loop inside. It's never exiting.
It's an infinite loop. So that last thing is very, very important. You always want to modify your loop control variable.
And again, the loop control variable determines the exit, in this case I.
So the for loop is used quite a bit.
It's very easy to use and it allows you to do 3 different things inside of loop.
Initialize the loop control variable,
check the loop control of variable for the exit condition
and then modify that loop control variable.
Those are 3 things that you have to do in a loop, and you can do in a single statement.
Now let me just talk about the infinite loop real quick.
So I just demonstrated an infinite loop. This is where you never exit.
And you have no obligation to put any parameters in the for loop statement.
You can actually write an infinite loop on purpose.
The way that you would do that is you would just put the semicolons in there.
So you would have a for loop with 2 semicolons.
If you ever see that, that means you're writing an infinite loop.
Now typically, you don't want to have infinite loops,
but there are times because the condition for continuing the loop is absent.
The loop will continue indefinitely.This is sometimes useful if you're monitoring data.
So if you're monitoring data, you might have something in there.
Typically, when you have a forever loop, you usually also have a sleep,
which will pause execution of the program, maybe for a second so you're monitoring something.
The other use case where you might have an infinite loop is if you're listening for a connection.
So if you're waiting and you're listening for connection or if you're pulling something
to see something you may do that.
Now some people argue that you should never pull and you should never
listen, but that's another argument.
That's really the only case for some for loops that are infinite.
Avoid infinite loops as much as possible by modifying that loop control variable.
I hope this was helpful. This is the for loop.
We'll utilize a for loop in one of our challenges, so we become more familiar.
Thank you.