OK, so really exciting moment.
We are getting to the point in our programming journey where our programs are about to get a lot, a
lot more interesting.
So why is that?
Because we are talking about loops and loops are really cool.
And the first thing that I'm going to introduce you to is the while loop.
So this will be the first type of loop that we talk about.
So let's get right into it.
So let's look at a familiar problem.
So this is something we've been messing around with.
It is this password problem where we're just trying to guess the password.
So a simpler version of this problem is right here.
We have tackled something that was a little bit even more difficult than this.
But the thing is, is that this is kind of annoying in general as a problem.
But the most annoying thing about it is that we have to keep running it over and over and over again,
right to be able to guess the password again.
Each of these little password programs we made, you had to basically rerun it, so you could then be
asked to enter the password again.
So that's super annoying.
So what if we can have a program that would just keep asking for us?
We didn't have to run the program again.
The program would just keep asking us.
We'd only have to run it once.
Well, that is possible, and it's something called a while loop.
And that's the good news is that we can actually make a program that uses this while loop and does this.
But I guess the kind of neutral that it neutral is, it's kind of confusing that might be good or bad
news for you.
I don't know, but you might not think it's confusing.
You might think it's confusing.
Let's see what it's all about.
So we can use this thing called a wire loop.
It is a programming construct that keeps repeating whatever you put inside of it until a certain condition
is false.
So vice versa, you know, it keeps going.
As long as the condition is true, it starts when the condition is false.
So yes, you heard that right?
I said condition we can use conditions exactly like the ones we've been using in our conditional statements,
like if else, if and else.
So just like if, for example, all the stuff that's in the parentheses it evaluates to either true
or false.
And if it's true, then we do the stuff in between the curly braces, right?
While loop is the same thing, except we don't only just do the stuff in the curly braces and move on.
If this is true in between these parentheses.
We're going to keep going as long as it's true.
We're going to keep repeatedly doing the stuff in here, hence the term, while while this stuff in
here is true.
Whatever this statement is available, as long as it evaluates to true anything in between the races
will be Iran.
And what'll happen is, let's say this is true.
It runs the stuff in here.
It gets to the bottom, it comes back up.
It says, Is this still true?
Oh, it's still true.
Yeah, I go do this again and get to the bottom and come back up.
Is this still true?
Yes, it is.
I do this again.
I go to the bottom, I come back up.
It just keeps doing that over and over until this thing in here.
In between the parentheses evaluates to false, in which case it will then move past the close curly
bracket and continue on with the rest of your code if there are other things in your program.
So pretty cool.
Let's go back to this program and see if we can change it up to use a while loop and have it ask us
multiple times with one run of the program without us having to rerun it all the time, right?
So I have this pulled up in Visual Studio here.
Here's that program.
So let's go ahead and just run it right now.
I think I've actually compiled it already, so I'm going to go ahead and run it, and I'll just say,
you know, pass and it's like, try again.
And it's like, Oh, I was wrong, I got it running again and I'm like, half W. and try again.
And I ran again.
And then I say, pass word.
And you guessed it.
Well, you know what?
That's kind of annoying.
We're going to put a while loop in here and we are going to fix this up.
So how do we change this to have a while loop?
Well, we got to think, what is the stuff that we want to happen inside the loop?
So this stuff, right, repeatedly do stuff in here.
What is the repeatedly do stuff in here that we want?
Well, we want to keep asking the user.
Right.
So that's one thing, keep asking the user for a password and we want to get that stuff from the user
when they type it in, right?
So keep asking the user right here, get stuff from the user.
Right.
So pretty simple.
That's what we want to keep doing.
So we're going to have these two lines inside of our wow loop.
So.
We don't necessarily have to deal with all of this.
I'm going to go ahead and delete this, and I'm going to get rid of this f right here.
And what I'm going to do is I'm going to say I'm actually I move this down and I'm going to say while
and what was this?
While input is not equal to password, then what's the stuff that we do into this stuff right here?
I'm going to copy this and paste it in here and dent it.
This is the stuff that we're going to do.
It's just going to say into the password.
So I can also put a little initial check here where I can say.
You know, into the password, I can like, ask them once and then I can use it, get line.
To get it and then what I can do is I can just say if
input not equal to the password, then I'll go ahead and do this while.
So I didn't that.
So basically, what's going on is that I asked the user for a password.
I get that password from them.
And then I checked to see if they have the right thing.
And what this really is is it's not just checking to see what's right as the right thing is basically
based on it being wrong.
So if it's wrong, if it's not the password, then we go in here and we say while it's wrong.
Keep asking them for the password.
So we can actually change this, maybe actually to try again, try again.
And then we'll keep we'll just keep getting it, and once it's actually correct, so once input is equal
to password, so this would be false, then it'll basically break out of this wow loop and then it will
go down here.
It's already this condition has already been decided, so it hits this.
And once it's done with this, it just continues on past this curly brace.
And that means that they would have guessed it right.
So we can just leave this down here because that means that in fact they did.
They must have guessed it because otherwise it wouldn't be done with the wow look, right?
We'd still be in the while loop if they hadn't guessed it yet.
So let's go ahead and try this out and see how it goes, so I'm going to compile it and I'm going to
go ahead and run it.
So enter the password pass, try in your training in the past.
Well, training in we're training in password, you guessed it.
Cool, so pretty neat, right?
So let's continue on learning a bit more about these lives, because there are some strange things that
can happen due to this nature of how it works with the condition inside here.
So hop back over to the presentation here.
So what about this?
I want you to make a new program that has a main function, and then it just only has this code in here.
So you're going to tape while true.
And see out, you can put anything you don't have to say, I'm unstoppable, but is kind of a hint at
what is going to happen, then go ahead and try running this program and it will be quite interesting
what you see.
So go ahead and pass the video and run this.
And then don't worry, you can play the video again, and I won't tell you how to fix what is probably
going to happen.
So if your program went careening out of control without any sign of stopping, you can always do something
like clicking in the console area and pressing control see, which means holding control and pressing
and see.
What this does is it kills the current process that is running and that process is your program.
So most likely, it's this unstoppable loop program that you've just done right now.
So as you continue through your program a journey, you will encounter this probably quite often.
Actually, you'll have these infinite loops and it won't stop and you'll have to control C and then
you'll have to go debug them and fix them so your program cannot get stuck there in that loop, just
bouncing from bottom to top.
So let's take a look at this.
So I'm going to go ahead, actually, and just do this.
In case you didn't run that, I'll show you, but you probably already ran it on this field, blah.
And I'll save it.
And then we will compile it, and then I will run it.
And it's just going, look at it blinking and you'll notice as I let it keep running.
Actually, I don't know if you'll be able to hear it, but my computer fan is starting to get faster.
The computers just smashing through various lines, printing them out, going crazy.
So yeah, if you get freaked out.
Just control C and that will stop it.
OK, so I'm going to get rid of that.
And I'm going back here to the presentation, and so we should make a modification to our loop condition
in our current program because you know what, it kind of almost is like an infinite loop.
If you make the user, keep having to guess, what if the user can't guess your password?
I mean, it's probably pretty hard, like they have no idea about any constraints about the password,
so it can just take them forever and ever and ever and ever.
And do we really want them to have to look up how to exit out of the program in the terminal?
They have to like control C.
That's not that great.
We probably should have an easier option for our users, right?
So let's make a modification to this program.
So I want to go back in here.
What we should probably do is say that they have an option to quit.
So what we will say is enter the password or type Q to quit.
How about that?
And then what we can do here.
Is I'll add this in here as well, try again or Typekit Q to quit.
All we have to do is change our while condition here.
So what happens is that the input is getting saved over and over again into this variable.
So when I ran it and I typed pass, it got saved in input.
You know, then it went up here and it said, is pass equal in the password?
No, it's not.
And then so it said, try again.
And then I entered past W and so passed W over wrote Pass like pass was starting here and now it's passed
w.
Then I go back to the top, it says is pass w.
Not equal to passwords.
Sorry, last time I said equal, it's really checking if it's not equal to password.
And it's like, yes, it's true, it's not equal, and then it goes again.
And I said past W.
Oh, and that got saved in here, so it keeps overwriting it right?
If we type Q, Q will get saved into input.
And then it will say, while Q not equal to password, is CUNA equal to password, well, yeah, IQ
is not equal to password, so keep going.
So what we can do is add something to this safe while the input is not equal to password, so it's not
the right answer.
And while
the input is also not equal to the screen Q because we're reading into a string right or reading into
a string.
So we want to check.
We want to loop as long as they haven't got the right answer, which is this.
And as long as they have not entered a queue because once they've entered queue, we want to stop,
right, then we're going to once we queue.
It's going to go back up.
It's going to say, Oh, input is equal to queue because both sides of the and this is the left side
of the end.
This is the right side of the and they both have to be true.
Both have to be true for this stuff to run, right?
So if this is false, true and false is false.
So it will actually stop the well, it'll continue past the F and it will say, you guessed it.
So let's go ahead and check this theory.
All right.
So I run this into the password Typekit account.
I'm just going to say pass again and then I'm going to say past W. and keep saying, Oh, and then I'm
going to say, You know what?
I don't know this.
I just want to quit.
I type Q and it just says, you guessed it.
But did we really guess it?
No, we didn't use it, right?
If we quit, we shouldn't be rewarded with guessing it.
So we should basically say something here that says if input is equal to password, then you can say,
you guessed it right.
So and else you can just say whatever you can just say thank you.
And again, not a semicolon, I cannot tell you right now.
OK, cool.
So let's save that unless we run this, OK?
That is a huge problem.
And the line could not use template parameter.
Oh yeah, it's just the Allen in line.
That's what I was forgetting.
OK, so we save it and try it again.
OK, so let's go ahead and run this, so yet this time, if I, you know, I do the same thing and I
say Pass W. and pass, whoa, and you go, I say to to quit and it's just a thank you, come again.
And it just might say, you guessed it.
OK, so that's kind of all I have to say about the wild loops.
This is going to get really interesting, though, because now we can do a lot more stuff in here and
that will help us actually solve some pretty cool problems.
So we will continue on with the wild loops and the next lecture.
And after we are kind of comfortable with, then we will move on to other types of loops.
So with that, I'll see you in the next lecture.