Welcome guys and in this lecture I want to discuss loops so loops in C just like loops in real life
right.
It's kind of like something that loops around the round and round constantly.
Well in C you also have loops so there's the full loop the while loop and the do while loop and we're
going to discuss them all right now.
So let's create a for loop here.
Copy what I'm doing and I'll explain it as we go along.
So you represent a for loop with a for keyword then you have brackets.
You have an initialization which is here.
You have a condition which is here.
And finally you have a loop which is here.
So what this means is create a loop that has a counter variable called i.
This equal to zero.
And you can see that's an integer and then it says the condition for this loop is the I must be below
10 and then it says at the next iteration of the loop increment I by 1.
That's where the plus plus means.
So essentially this this this loop will loop 10 times so we can demonstrate this by doing print f II
and a new line.
You know that the uh that the backslash n for new line and then I am here now if we compile that
work may not see guys not CBP means C and if you type main you can see it says 0 1 2 3 4 5 6 7 8 9.
So as I put it that 10 times because I starts zero here.
And you know this could be below or equal right.
And then we would see 0 1 2 3 4 5 6 7 8 9 10.
So you know that the condition is basically an expression much like explains in the if statements where
you can have equals equals does not equal above and all that sort of stuff.
So these are basically for loops.
So what is basically happening is anything between these brackets here which is the scope by the way
you know scope is anything between these brackets so anything between these brackets of the for loop
is basically being run on a loop.
So what happens is we come in here we create a variable called i we set it to zero.
We then check if I is below equal 10 which is.
So we start running the loop.
So we we basically print print the eye the eye Variable value right.
Then we leave the loop causing I to increment by here.
So then I becomes 1.
So then right now I is one year and then we check this again is I blow equal 10 will I is 1.
So it is below equal 10.
So then we run this again.
We output the IV out the eyes value and then we leave the loop again.
We increment i now i is equal to 2 and we go on and on and on until this condition is no longer met.
And then the loop ends.
So that that is basically for loops for you.
So let me show you how you can count backwards.
Let's set eye to 10.
And then we want to change I to say above zero and then change the I plus plus I minus minus.
Now if we run the compiler first you can see Ten nine eight seven and so on.
Now how that works is we set eye to 10.
We check if i is above zero.
Well I is 10 so it is above zero.
So we output at the eyes current value and then we decrement I by one.
So now I is 9.
I is still above zero because I is nine right.
So then it outputs this again.
So follow ups are very powerful and they used all throughout the C language when you were making projects.
OK let's now discuss the while loop.
So get ready to follow and we're gonna go while one.
So what this does is this runs everything in here so long as the condition between these brackets is
true.
So remember what I said before 1 is true.
So everything in here will be run forever.
If you do print f hello world and you compile that
I knew two main
you can see it says hello hello hello.
Constantly.
Really fast.
Right.
So that is while loops for you.
So if we wanted to do something like this similar to our fault we could go into I equals zero while
I below 10.
And again this will be infinite.
At this point currently because I 0 so below 10 right but now and here you can go I plus plus and now
if you was to compile that it would work similarly to the for loop you see one two three four five six
seven eight nine ten is output is hello world ten times.
So that last while loops for you but you can also in and this applies to all the loops by the way you
can leave a loop early for whatever reason and you do this by using the brake key.
So for example let's say if I is equal to to break and that's another thing by the way when using f
statements or any statement told you don't actually have to use the curly braces if your if your code
is only one line long.
So in our case we only have one line with the if statement so we don't actually need these and it will
run that one line and leave if you need more than one line.
Let's say we wanted to output a message here and break.
Then you can't do that.
You need to use the curlies but for one line you can use it.
So essentially if you if statement is only gonna have one line inside the body between the brackets
between the curly braces then you can use just on the line on.
So without these curls literally just like that.
And that would work fine.
So what this is gonna do now we're going to come in here and as soon as I is equal to two we're going
to leave this loop.
That's what the brake keyword does.
So let's run that now compile it first.
You can see it says hello world hello world hello world.
So as soon as I is equal to two we break from the loop.
So in addition to break you also have the continue key word that allows you to skip over an iteration.
So I'll show you how to do that with the for loop
so watch this then we're going to print out the index as before
and I'm just going to compile that to show you that that works fine.
You can see that 0 1 2 3 4 5 6 7 8 9.
Let's skip over three.
Yeah.
So if I equals three continue.
And now if we compile.
Now we can see it's skipped over 3.
We have 0 1 2 4 5 6 7 8 9 3 is an output it.
Now why is that.
Because we go in here we say if I is three then continue to the next iteration of the loop.
I want to leave this loop for this situation and pretend it never happened.
So then it immediately skips straight to I plus plus and stops executing here and then obviously I then
becomes four and the loop continues is I equal to three.
No it's four.
So continue is never cold again and we print as usual sort of the final loop.
I want to show you is the do while loop so less to do.
Oh and I filled it in for us.
Brilliant.
So essentially it's similar to the wall loop apart from the condition happens last with a traditional
Wall loop.
What happens is we say while this expression is true run this code whereas in a do while loop we say
run this code.
And if the expression here is true.
Run it again.
So that's the difference here.
And just to demonstrate this if I do print f first loop and then I do print f.
Second loop.
And now if I go to here and I compile again I run that you can see second loop is output it let me just
do a break here as well so we can leave because that's currently an infinite loop right.
So you can see the only second loop is output it.
And why is that.
Because here it is false.
So this never gets run but with a do while the codes run first and then the expression is checked.
So if this was zero and we tried that again we can see second loop still runs even though it is not
even though it is not true.
And just to get rid of the break as well to show you that isn't whilst leaving you can see second loop
still run.
So essentially the expression has zero but because the codes run first it doesn't reach this expression
until the end.
So it knows not to loop back around but it will at least do one loop because it's a do while whereas
the wild expects the expression first and because it's zero it never gets run the do while runs the
code first and then expects the expression which is why we get second loop even 1 0 is here.
If you have true here it will be an infinite loop.
So that's two while.
And the difference between while and do well.
So I hope that makes sense.
Please send me a message you have any trouble understanding that.