OK.
Welcome back to another lecture.
So in the previous lecture, we went over kind of the theory behind logic and using some Boolean.
We talked a little bit about truth tables and we kind of introduced one tiny part of what conditional
statements are in C++ and just programming in general.
You notice in the last lecture we use that word, if that is a conditional statement, part of a conditional
statement.
So in this lecture, we're going to be going over kind of how to implement all this in code.
So we will be going over Boolean and code, which you've already kind of had a little bit of an introduction
to.
But we'll be using billions to satisfy conditional statements that can be evaluated to true or false.
And we will be also looking at various operators that we can use inside of conditional statements.
And we'll be looking at those keywords as well, like the if.
And there's also some other ones that we'll be using, like ealth and elusive.
So with that, let's go ahead and get started, so I'm definitely going to want to print some stuff
out to the console here.
So you notice I have a terminal open PowerShell terminal.
I've also already made a file conditional star copy.
I just put it in this first program directory, even though it's not our first program and went ahead
and left it in there.
So if you do not have a file update, go ahead to file and make a new file.
And if you don't have a terminal going and you need to compile your code in a terminal, then you can
go up here to terminal and the terminal.
And like I said in some of these lectures, I'm going to be using Visual Studio similar lectures, I
will be using Sea Lion and sometimes I'll just be using text editor like them.
So in the command prompt or just this poem?
All right.
So let's go ahead and get started.
Like I said, I want stuff to print out to the console, so I'm going to include Io stream here.
I'll do the standard using namespace and I want you into main and make it main function here.
So the first thing that I'm going to do is make some variables and those variables are going to be Boolean
to start off with, so I'm going to make a Boolean and I'm just going to call it be one.
I'm going to set that to true.
I'm going to make another Boolean and call it B to set that to be false.
I also will go ahead and define some integers here so I can use them and compare them and some of our
conditional statements.
So I'm going to make an integer, and I just call this i1.
I'll make you do it five.
And I'll say and and I to and I'll make this equal to 10.
OK.
So the first thing that I want to do is kind of go over some of the operators that we can use in C++
and what we're talking about today is going to be the logical operators.
Operators are something like many you see out.
Remember, I have to put these.
Two things right here.
So this is that output or also known as the insertion operator.
Remember that I was kind of saying that what happens is whatever you put on the right side of it, it
kind of goes into these open open mouth and then goes out to the console out right here.
So this is an operator.
Another operator that you've already seen is the assignment operator, which is this singular equals
sign here that assigns the value on the right side of the operator to the variable on the left side
of the operator.
So we're going to be talking about some more operators, and those are going to be things like.
Two equal signs.
And.
Two of these pipes to ampersand, and we'll also have something like a greater than or greater than
and less than greater than or equal to less than or equal to.
So we have all of these right here.
So let's go ahead and start out with this one right here.
So this is two equal signs.
So what does this mean?
We already have seen one equals sign.
Well, this one equals sign.
Like we already said, it's only used for assignment, so take whatever is on the right side of it and
put it in the variable to the left.
The equals sign is for actual equality when you want to check whether something is equal to something
else, whether they're the same thing.
So that would be, you know, something like five equals five, you know, seven equals seven.
Things like that.
Another thing that I can add in here is when something is not equal to something else, when something
is not equal to something else, you put an exclamation mark and an equal sign like that.
So if you want to check whether something is not equal to something else or, you know, make some sort
of statement, it would be like, you know, for no equal to five something like that.
So I introduced these two already, I'm going to continue on with these as well.
But something that I'm going to do first is kind of bring back this if that we talked about in the last
lecture.
So this if you just saw it in a theoretical sense, but it's actually a key word inside of the C++ language,
and it's the most basic kind of building block of conditional statements.
So if you want to check whether something is true or false, you can use this if and what you do after
typing.
The word f is you put a pair of parentheses similar to like what you have here after main.
And then inside of the parentheses, you put one of those conditional statements, so kind of like we
had in the last lecture.
You know, if P or Q something like that?
So let's start off with something basic using these operators that we have right here.
So inside of the parentheses, I'm going to say if five equals equals, we're not five.
Yeah, I will say if I one equals equals five.
So here's the statement I'm saying I'm putting the word if I'm putting some parentheses right here and
inside of the parentheses, I'm taking my variable.
I won and I am checking to see if it is equal to with this double equal sign the kind of hardcoded value
of five, which it is right.
I one is five because five are stored in into one right here with the assignment operator, we made
a new variable, a new integer variable on this line right here.
So this if statement will evaluate to true, because i1 is equal to five.
The usefulness of it, though, is to do something when i1 is equal to five.
So the conditional statement can be used and particularly if statement can be used to activate some
lines of code.
If this is true, so it's kind of like checking on something and then using the answer, whether that's
true or false, to decide whether you want to run some of the code and the way that we run or the code
is just kind of how we have these brackets for main.
You notice that when the main function gets called Ember, we said the main function is called the operating
system.
All of the code in between these brackets.
So you have the clothes here in the open here.
So all in this code gets ran right when the main gets called in, kind of think of the F as running
as well, just like main, if this is true in here.
So if I one equals equals five, so if I want is equal to five, if that is true, then whatever is
inside of the curly braces is going to run.
So whatever code we put in here will run.
Otherwise, it will not run the code.
If this is false, it's not going to do anything in here.
It's actually just going to continue on to whatever happens past the closed curly brace.
So if you had more code down here, it would just jump from here.
Oh, that's not true.
I'm just going to continue on down to here and it would run this next line.
So since kind of we have this pattern of just printing stuff out, let's go ahead and continue with
that.
So I'm going to just say, see out, I equals equals five and I put an end line there.
And then I'm actually going to before I run this, I'm going to introduce some kind of another topic
kind of randomly that may or may not have been introduced yet, and that is something called a comment.
So this is just a side note kind of a thing.
All of this right here, I would kind of want to keep it here so we can refer to it.
Want to be able to look at all these symbols and explain them, but I don't want C++ to hit this line
and not understand what's happening.
I don't need C++ to run this.
This isn't like code, right?
This is just some sort of visual reference that we have that is being used to help us write some code
whenever we want some sort of note or some sort of line.
That's not runnable code, but it helps us understand our program.
We can put something called a comment.
And the way that you do that is just two of these slashes, you notice that this turns gray now.
So this is totally fine, and C++ will see this, and it'll be like, OK, everything past this on this
line.
So all this highlighted section is not really code that I should try and compile.
It's really just a comment that the person put in here so they can better understand the code.
It's like a note.
So I'm going to go ahead and leave that there.
You can put comments wherever you want.
Like, for example, you could say and make some integer variables.
So that's what we're doing below this line eight, right on line nine and line 10, we're making some
integer variables.
Here, I could put another comment and say, make some alien rebels.
And you'll notice that these are very important things for writing code.
It's very good to come in your code because if someone else comes to look at your code, they might
not understand what you're writing as far as the actual code that runs that part.
And it's really good to make it easier for them to understand your code.
So it's good to put comments, not everywhere, because sometimes it is obvious what a certain piece
of code does.
As you progress throughout learning programming, you'll notice that you'll you'll somehow be able to
tell the right places to put comments.
It'll just feel natural, like, Oh, this doesn't really make that much sense like this function doesn't
make sense.
This if statement is not so straightforward, maybe I should put a comment to say why I created it or
what the purpose is.
What's going on here?
So.
Side note here's some a way to make comments really helpful so you can go back and look at your own
code as well, or someone else can look at your own code and understand it better.
So with that, let's go ahead and get to it.
I'm also going to put a return here.
Here is the lack of it because we've kind of done it fast and it's good to put that kind of convention.
So let's go ahead and run this, we have to find some Boolean variables, we're not really using them
quite yet, and we've defined an integer and another integer here and we are using this.
And I one to compare it to the value of five and try and get this to.
So what should happen is this should print out, right?
Because this condition in here is true.
So let's go ahead and test it out.
I am in just this users Dylan directory, so I'm going to need to navigate to this directory right here.
And that's where this program will exist once I save it.
So I'll go ahead and save, or you can just control us.
And I'm going to use the CD command, which has changed directory and I'm going to go into first program.
So now if I do a tour, I can bring this up and I will scroll up a bit and we have some other things
in here, too.
I also already have an executable in here because I've already tested this out, so I'm going to go
ahead and remove that.
And if I do another tour that it's not there and then, yeah, now I have this conditional slip.
So I am in the right directory that has this file that's been saved.
Cool.
So.
Let's go ahead and run this code, then we'll go ahead and compile it and then run it.
So remember what we're going to do is a G Plus Plus and then the name of the program so conditional
start CPP and then I'm going to do a dash because I want to name the output file and I'm just going
to call this con e e.
So that'll be my output executable file that we can run after we compile this press into.
So it looks like a comp. I'm going to do there, or if you are on Lennox Al's PowerShell, you can do
either one.
So I'll do it last there.
And it looks like it was successfully created the executable file.
Go ahead and clear.
And then now I can run that, so what I'm going to do is just to the slash, and then I would do kind
of go easy.
And we will run it.
And if all is good, we should see this show up down here.
OK.
And we do see it show up because I one is in fact equal to five because we stored five and I won.
All right, so let's go ahead and just change this up so we can kind of see different results.
So if I put it to here, I two is 10, that's not equal to five, right?
So I'm going to press up on my keyboard and go back to the compile statement here so I can just read
compile it easily.
Actually, I forgot to save it, so I'm going to do control SE, make sure to save it first.
Now I will re compile it and then I can go up to run it again.
And you notice after I run it, it just goes to another promptly and doesn't print anything out.
That's because I too is not equal to five.
So what what if we see if we can get this to print out again?
I'm going to call it two, not equal to five and then let's go ahead and use this one right here.
So this is the opposite of this.
It is not equal.
So when something is not able to something else.
So let's go ahead and put this here.
So as long as I2 is not equal to five, this will evaluate to true and then it will run this code in
here.
So we have an issue not equal to five in here now, and I just change this from equal, equal to not
equal.
So let's go ahead and save this, so control Earth, and then I'm going to recompile and then let's
run it.
So now we notice that it does print this out because Ita is not equal to five is a true this is a true
statement.
Everything that's highlighted here.
I, too, is not equal to five, because I too is 10.
So pretty cool.