The saw the last section the pattern for control flow where it took different actions based on a condition.
So look something like this.
We had the word in the parentheses which contained the condition that condition of course evaluates
to TRUE or FALSE.
And then you had the statement block where you had all your statements
in the last section.
This was the if statement and the switch statement.
But in this section we want to take a look at how we can do statements multiple times.
And if you remember in the first section we call this a loop.
So in this lecture I went to go over what's called the while loop and the while loop has this syntax.
So the key word you just have the key word while while here and the while has a condition just like
an IF statement.
But this condition is really what I talked about in the first section which is the stopping condition
or the condition which is long as this is true.
It will do the statements essentially.
So remember that the we have to have we have to have this stopping condition otherwise the loop will
go infinitely.
So the dition is really any boolean condition you want and here's how I kind of think about it.
So while the condition is true let me just put true here.
It will do one iteration that is one set of all of these statements so do these statements one time
and then it'll go back up and check the condition again to see if it's still true.
And if it is it will again do one more iteration of all these statements.
But if it's false it'll sort of exit this while loop.
It won't do any of the statements in this statement block and it'll just continue on doing the rest
of the program.
So let's take a look at an example.
Let's say we had a variable I would call or set it to be 1 in one of the constants and we'll call this
maximal set to 10.
And the condition is while it is less than or equal to 10 or Max don't use magic numbers.
So it will just say seek out the current iteration is an analyst say I.
So really just printing out 1 to 10.
And then here we need the we need to increment I because otherwise if he didn't i will always have the
value 1 and since one is less than or equal to Max this will always be true.
So we loop infinitely if we didn't have this I so this really controls when the loop stops.
Plus plus in its run it and see what happens.
So here we go.
So says the current iteration is 1 and then 2 and so on up to 10.
So one thing to note here is when just after Princess this last line here and the I variable is 10 so
when it's 10 it'll print this out.
And then it will increment the variable which will be 11.
Now it'll go back up to the while condition and it will actually check the condition one last time.
So 11 is not less than or equal to 10.
Therefore this condition becomes false and then it will break out into the rest of the program.
And so it always does it one more time to check to see if it's false or not.
So here
the the while loop is really just another control flow structure but instead of making one decision
it really does make the same decision multiple times until it's false.
That's kind of the gist of the While loop.
So let's see another example.
So let's say we had some
in it to be zero for now and we have Max to be 100.
And what we're going to do is we're going to sum up all the even numbers from 1 to Max.
So do this
say if it is modulus called zero.
So this means if it's even if it is even then right some are going to say plus equals.
So this going to sum all the even eyes and then we're going to increment K and we're on to see out some
of all even numbers from 1 to and really see Max here.
Is
some
in is going to run this.
So the sum of all even numbers from 1 to 100 is 2500 and 50 and notice we're using a constant.
So you change this in you one to 1000 in there you go see.
See the power of Constance.
We to change one thing and then it changed the rest.
That's why we introduced Constance her useful so the while loop really is the mother of all loops.
And in fact we don't really need any other loops at all.
However there are some that are make things a bit more convenient and Runcie to other loops at have
some special purpose or make things a little bit different than the wild.
And we're going to see that in the next lecture.