OK, welcome back, everybody.
So we are continuing on to have this discussion about while loops and just loops in general.
So what we're going to be talking about in this lecture is something called a dual loop and something
called an accumulator variable.
Those are the two main topics.
I'm not going to go into a slide show today.
We're just going to hang out in the ED and kind of mess around with some stuff.
But before I get into those two new topics, I am going to kind of go through the code that we wrote
in the last lecture and kind of clean it up a little bit.
So we're going to talk about some things that may be redundant.
Some ways for us to minimize the amount of code that we have and talk about kind of just coming up with
a solution and throwing everything you know at something and solving the problem and afterwards going
back and kind of like making stuff look better.
So that's what I'm going to do right now because our program actually ran fine.
It did what we wanted it to, but it kind of has some excess stuff in here.
And so we're going to talk about how to get rid of that excess.
All right.
So let's go ahead and get started.
So one thing that I am going to point out right off the bat is that the other day I went ahead and in
that last lecture, I wrapped this whole while live in this.
If so, this while loop is now nested in the scope of my.
If so, this indentation layer and these brackets primarily in C++, it's about whatever is inside the
brackets would be considered to be in the scope of those.
So this highlighted stuff is in the scope of the if it's inside of the if you may have been wondering,
well, why did he put this input not equal to password?
You notice that when highlighting this, it's highlighting it right here.
So we already have this same condition being checked in a while.
Yet I still wrap this issue around here, and that's a good observation because it's completely redundant.
One thing I wanted to illustrate in the past couple of lectures was just about, you know, using the
stuff that we've learned so far.
And the big thing was being you knowing that you can actually put things inside of other things, the
fact that you can put a while and then if you can put another if in an if you can put a F and an F and
an F and an f over and over and over again, as many layers as you like, you can put all these things
inside of each other over and over again.
So that is possible.
And I wanted to point that out.
But as you notice when I highlight this, we're checking the same condition.
This condition is already here.
We don't need to check it once and then immediately go to this line and then check it again just to
be able to run this code.
We could just only check it this one time.
And then if you know, if this is true and this is true.
Doing condition on this coat.
Right, because if the user enters, you know, cue to quit.
Like or their you know, they enter the right password, so it's not not equal to then we would just
bail anyway, so we don't need a double check for this, is what I'm saying.
So let's go ahead and get rid of that right off the bat, right?
So we're going to clean this stuff up, you know that.
It's good to come up with some sort of naive brute force solution.
If you're just trying to solve a problem, you just want to get it done right.
Just like do whatever needs to be done to solve the problem, but then afterwards, you can go through
and make it more efficient, and this is something that we are going to focus heavily on in the next
section of the course, the second part of the course, which is on algorithms and stuff like that.
So we're not only going to make our code more efficient in terms of how it looks and how many lines
there are, but we're actually going to go over the efficiency of how fast our programs run.
So that is something we'll get into later.
Right now, we're just trying to clean the code up.
We don't need redundant stuff.
So we got rid of that.
Um, let's see.
There's still some other things we can get rid of.
One of them.
I mean, this is kind of annoying, right?
We had to add this stuff in here because what we had before was just this.
We had that.
And we didn't have that, you know, we didn't have this stuff down here, we had to add it because
the problem was this when the user pressed Q. Of course, it stopped doing the while loop, and it just
continued on past the while loop.
And then it just said, you guessed it, but we didn't want to say you guessed it right.
Because if they press Q, they didn't actually guess it.
So that's why we ended up wrapping it in this.
If and then we put this elsewhere, if they didn't guess it, then we just said, thank you.
Come again.
It would be a lot nicer if we could just stop right away if they pressed Q.
I mean, if the user presses Q, it says Q to quit and honestly, the program should just stop.
We don't need to do anything else.
We should just straight up stop it right away so we can actually do that.
So what I'm going to do is put an f here, so I'm going to check to see if the user entered Q.
So I'm going to say and I'm actually going to put that after this get line because we want to make sure
that we actually get that line in and see what it is.
So right here, I will say if input is equal to Q, then we can use this special function.
Just like that line, I'm introducing another function.
Yep, I know I've introduced a lot and we still haven't gone over how to make our own functions, but
we need to know about the built in functions to do a few things before we get into making our own.
So this function is called exit, and it does exactly what you would think it does.
It exits the program immediately.
Once it hits this, it stops.
So pretty cool, right?
So what we can do is similar to the return zero.
We can put some sort of number in here.
You remember how I was talking about returns zero from Maine.
When the program stops, it kind of returns control back to the operating system and it gives this zero
as like a successful exit code.
We can put an exit code in this exit function.
Sometimes if something goes wrong, you put like a negative one or some other number in here.
It's really up to you, and I'm going to go ahead and put this zero in here and we finish it off with
the semicolon.
Let's go ahead and check this out now.
As I commented this stuff out here, we should not see a, you guessed it, print if we enter a queue.
So I'm going to go ahead and compile this and clear this, pull it up and we'll go ahead and run it.
So I'm going to enter past just the wrong thing right away.
So it goes into the wild loop, at least says try again, so try again or Typekit to quit now and then.
What I'm going to do is I'm actually going to press Q and you notice it just stops.
It does not do anything pass.
Then, you know, I could put anything after this.
I can just even say, like, see our test, you know, something like that.
I save this compile run again, cast queue.
It doesn't even print this thing.
So that's that's one way that you know, that it for sure is not reaching anywhere below this exit.
So pretty cool, right, except there's another way to do it.
We don't have to put an exit.
We can actually just use this because Return Zero returns to the control of the program back to the
US.
Basically, it's done at that point, right?
The main function is the entry point into the program, and it's also kind of like the exit point here
at the end.
Everything that matters happens in between here and here.
Like this?
This zone is where the other stuff that we tell it to do happens.
So what I can do is I can just kind of prematurely exit as a press cue, I don't have to do anything
else, and I can do that by just returning the control from the function immediately here so I can just
put a return zero here, and it would do the same thing.
So let me go ahead and save this and then compile it and run it.
And so I'll say pass, and then I say Q, and you notice it's the same thing.
It just stops right away.
So pretty cool right now we can actually get rid of a lot of this stuff.
It looks a lot cleaner if I just delete this.
You know, it's our code is quite a bit more compact now.
So we can actually even get better than this.
And that is using this new concept called a dual loop.
So in this while loop, you notice that we actually had to do this first to be able to check the condition
here.
So basically before anything inside the wire loop like this stuff, all of this stuff happens.
We're having to check a condition.
And so that's why we have to the the thing is that the condition is based on the input, right?
It's just declared here, but we don't have any input until we get input here.
So that's why we actually have to ask the user to type something and then get it before we can actually
check the condition for the while it.
So it would be cool if we could just not have a try again, like if we could just have one line that
just said like enter the password of Typekit to quit, and it just kept saying that over and over again
for you to enter your password.
We could make it even more concise, and we wouldn't have to have a get line here and get lying here.
So there is a way we can do this, and it is something called a dual loop, and basically it just does
kind of the same thing as the wow loop, except it lets you do the stuff inside the while loop and then
checks a condition below that to see if it's going to continue doing it rather than immediately checking
the condition.
Which this is contingent upon, you know, that while it won't be entered unless this is satisfied,
but what if we wanted to like ask the user to enter our password and then get a line?
And then we wanted to say, OK, if they entered the wrong password, I want to go back above this and
just start and keep repeating it as long as they keep entering the wrong password.
This is what the dude does.
So we can actually change this.
I'm going to actually just comment this out for now.
Another block comment.
Kind of like you saw down there before when I commented out these, if that's something I showed you
already, remember these slash star in the star slash on the other end of it.
So let's go ahead and do this do well.
So I'm going to say do like this, and then I just put a bracket and then I press this enter key here.
And then what we're going to do is this stuff, right?
So we actually want.
Kind of something like.
I'm actually just going to do this.
Let's see if they enter the password
and then we'll get line.
And then we probably also want this right here.
So if it's cue, we can just, like, return immediately and then what we do with the wow condition
is it comes right here after this close bracket, so we say do and then while.
And then it's going to be this stuff right here.
So I'm going to copy this and paste it in here.
And that's our condition, right?
OK, so pretty cool, pretty cool.
So what we do is we actually can run this right away.
To get at the initial time, if it's cue, we immediately bail.
Otherwise, if it's not cute, you know, we're going to go down here and we're going to end up checking
this condition, well, you know, we'll be like, Yeah, is it not equal to password or is it equal
to password?
And we, of course, don't want it to queue to be queue.
This actually would not even be 100 percent necessary at this point because it would just.
Already break out of it so we can even reduce this and just say, well, if it's not equal to password
because it's already returning right here immediately, and then we just have the, you guessed it,
down here.
So let's go ahead and compile this.
All right, so all looks pretty good, we go ahead and run it, so says into the password or Typekit
quit, let's say S8 Pass says enter the password Typekit a quick past W password and it says, you guessed
it, let's go ahead and run again.
So I'm going to first queue it just immediately quit.
I'm going to run it again.
And we actually it know I want to steer clear and then I'm going to run it again here.
So cue immediately made it quit.
So that stopped it.
And then if we just do password right away, it just says, you guessed it, so, you know, it has
the same functionality and it's a lot more it's a simplified quite a bit more now, so pretty nice.
Now we have something pretty succinct.
So let me like delete this stuff here.
I'm going to delete this.
I mean, look how small our program is now.
Compared to how it was before.
Like this is pretty, pretty compact.
So the dual is something pretty cool.
The main thing I wanted to point out about it.
Is the fact that it lets you kind of.
It kind of flips it around if it flips like the the control flow style.
So it basically lets you do something before the condition gets checked.
So it's like, OK, go ahead and do this, and it's like telling the program to do something, it's
like, Okay, do this.
It's like, All right, I'm going to do this.
And then it gets to the bottom and it says, like, OK, we'll keep doing it.
This is kind of instead of while it's kind of saying like, keep doing.
Keep doing as long as this is true.
Right, as long as input is not able to password, then keep doing the stuff that's inside the brackets
of the do.
So that's, you know, something that's pretty cool.
Not necessarily like always going to be something that you're going to use like a whole lot.
Some people love do loops and they're just using them constantly.
I don't think it's necessary all the time.
You can definitely use a while loop.
Quite often, and of course, you notice it wasn't necessary to solve this particular problem, like
we were able to solve it with the while, this just kind of helped us light compact our code a little
bit.
There will be some situations in which the duo will make it a lot, a lot easier and will make a lot
more sense.
So you might encounter situations like that where you definitely want to be able to at least execute
some chunk of code once before having to check the condition.
This could depend on some sort of variable here that gets initialized to something and you want something
to happen and maybe the variable gets changed in here.
And so you want it to at least happen once first, rather than relying on a while where it might not
execute the while loop at all because of the condition of the variable or something like that.
Whatever was stored in the variable was contingent on like this thing passing, so it wouldn't go into
the well, that's like kind of maybe an example where you used to do well, you'd need to use it.
So that's pretty cool and all.
So now I think that we're going to move on to something called an accumulator variable, so this is
something when we modify our variable to kind of make the loop based on that.
So that's kind of what we're doing already, though, right?
We are modifying this input because.
We asked the person for the input kind of over and over, and so the variable called input changes like,
you know, it was it was passed and then it was like passed W.
And then finally I entered my password and it actually stopped and went down here and said, you guessed
it.
So that means that this variable had stored each one of those at some point in time, which is overwriting
it.
This is something I mentioned in the last lecture to.
But we can do some other things, too, like let's take a sample.
Let's take a sample problem.
So I'll leave this do well here, and let's change this problem up a bit.
In fact, what I'm going to do is I'm going to copy this over to another file.
So I'll actually say
C.P..
Wow.
Stratigraphy, maybe I hopefully I can do this in PowerShell.
This is something I would normally do in Bash.
So and I'm going to call this a see.
Wow, let's just call it wow.
Looks to CBP.
OK, cool.
So it seems like maybe I can open this now.
I'll go ahead and.
Open this file.
So I'm going to make this other file is basically a copy of it.
And then I'm going to modify it a little bit.
So what we're going to do is instead of having the user enter a string.
Let's go ahead and have the user actually enter a.
No, I actually keep entering numbers for that matter, so I'm actually going to say and no like this.
And what are we going to want to do is like here, I'll put the problem statement here, so we'll say
make a program that takes input from the user as an integer repeatedly.
And stops when a specific sum has been reached.
So let's say we have some thing here this like and some equals 60 or something like that.
And then basically, we're just going to have the user enter numbers.
Over and over again until.
We reach 60.
And then we want the program to stop.
So pretty simple program not, you know, doesn't really do a whole lot of interesting stuff, but how
would we solve this?
So right here, I made some variable called some variable that hold 60.
I've also declared a number variable that has no value yet.
And so now we're going to be reading into this number, so what do I do instead of?
Get lean, I'm just going to see an end to you.
No.
And so we should loop as long as the something, you know, the Sun or whatever, you know, the users
entering is, is and hasn't reached 60 yet.
So how would we do that, though?
So why don't you go ahead and pause?
So I'm going to go ahead and get rid of this.
Let's save this something, we get rid of that stuff there.
Why don't you pause the video and see if you can actually figure this out?
So I'll go ahead and try and solve it now.
If you figured it out, that's great.
Otherwise I will go ahead and kind of walk through it.
So what would we do?
What do we do for this?
Well, we have the Sun set to 60, so we don't really want to change that variable.
But what we can do is actually use this concept of an accumulator variable.
So accumulator variable is kind of like they take that word as it is.
It accumulates stuff, you know, you're kind of adding more and more to it.
It's kind of like a snowball rolling down a hill and getting bigger.
It's accumulating more mass.
So I'm going to do something like that in the form of a variable that actually holds the currents.
And I'm going to call that total.
And what I'm going to do is I'm going to set that equal to zero to start off with.
So Total is going to have the value of zero.
And then my strategy is going to be make this wildly based on this variable.
And we're just going to keep adding the user's numbers to it.
And what I will say in the wild part is I will say if I will say while some so not some so well, total
is less than some.
So I'm going to keep going as long as the total is less than the sum that we've defined.
So the users will enter these numbers.
And then what I'm going to do is after they enter it in here, when we get the scene in and we read
it into, no, I'm basically going to say total plus equals.
So we're using this plus equal operator to add to total.
It's the same thing as saying total equals total plus something MSA Total Plus equals no.
So we are adding this number to total, and so we just keep adding to it.
It's basically like we're summing up all the numbers.
That they're entering, and we're going to keep doing that as long as it doesn't, you know?
Get to the same amount as or exceed some.
So pretty cool.
And I'm not going to say into the password or Typekit to quit, I'm just going to say, let's just say
insert a number into a number of Typekit to quit.
So let's see what this will do, so we have our son, which is 60, right?
Then we have our number, which is just declared and not initialized, and we have this total initialized
to zero.
We go in here, we say, Hey, enter a number Typekit you to create.
Let's say the user enters a number like 10 or something and reads in to the number and total is now
zero plus equals 10.
So that's saying zero.
Plus, ten total equals total plus 10.
Now it's gone from zero to 10.
They have an intern Q So we don't return.
It's total less than some.
Yes, it is.
So we go back and we ask again.
So let's go ahead and check this out, so I'll go ahead and save it, and then I'm going to just do
this while loops to that speed so that we are able to see.
So I put in extra parenthesis on here, it looks like it's an expected prime primary expression before
that token.
That just means that I had an extra parentheses.
You notice it's pointing to this right here should not have had that there, so I'm going to save it
and compile it again.
We have another problem.
Input is not declared, so I need to change this to no.
And we're actually reading in an integer, so I shouldn't really I mean, the chars and integers are
fine, but I probably shouldn't really have that.
So what I'm going to do is, let's just say.
Or
Typekit zero to quit.
How about we do that?
So we're not going to like add zero to it, so we'll see if no equals zero, then it will quit.
How about that?
It's just to demonstrate, you know, this this program, I know it doesn't make a lot of sense, but
you know, it'll be nice to Typekit, but I don't want to get into how to, you know, read in these
characters and convert them to integers.
Quite yet, that is something we're going to do in the future.
So for now, I'm just going to say enter a number or type zero to quit.
So I will say, let's just say enter a positive number or type zero to quit.
So we'll say that I'm actually going to go compile that, and now it compiles.
OK, so let's go ahead and run this, so into positive number of times here to quit.
What if I just enter 60 right away?
So you notice it just stops.
Let's try this again at five 10, 10 five five thirty eight quits.
So pretty cool.
These are actually these accumulated variables are very useful for a lot of the kind of slightly more
trivial but still important problems in computer science.
So a good thing to know when you're basing a loop on a variable.
This is just kind of good strategy, like if you have some question that talks about changing the state
of some variable or needing to keep track of a sum or something like that, you can then immediately
think about the fact that you have a tool, which is a wow loop and you're like, OK, I can use a loop
and then I can make the loop based on that variable that changes.
And then I just need to think of a condition, right, like I need to keep doing stuff as long as this
variable hasn't reached a certain value or something like that.
So you keep adding to it, maybe you keep subtracting from it.
Maybe you're concatenating to a string.
Or maybe you're putting a bunch of stuff in an array or something like that, just like it's a really
good problem solving strategy to have in your bag this accumulator variable.
So we'll be doing a lot more problems with this, and we'll come up with some slightly more complex
ones so you can practice and get better.
So that's kind of all I had for this lecture.
We're going to continue on with loops, but we're not only going to look at well lives, we're going
to start talking about another type of loop.
And we're also going to involve arrays in this as well.
So with that, I will see you in the next lecture.