In this video, we'll learn about the c++ if statement.
The if statement is the most basic of the c++ selection statements
and his syntax couldn't be any simpler.
We start with the if keyword.
And this is followed with the control expression in parentheses.
The control expression must evaluate to a Boolean true false value.
If the value of the expression is true, then the statement is executed.
Otherwise, the statement is skipped.
Note that the statement is indented.
We indented our code in the main function
but indentation is a very important element of style
that we need to start getting used to right away.
By indenting the statement, it makes it easier for someone reading the code
to see that the statement is part of the if statement.
Remember, indentation and style is just for us human programmers
to help us see the structure of the code.
The compiler doesn't care where you place the statement.
So you can see how simple an if statement is.
Let's see conceptually what's going on in the if statement.
Suppose we have this code. If num is greater than 10,
increment num by 1.
The expression num greater than 10 will be evaluated to true or false.
If it's true, then num will be incremented. If it's false,
no increment will take place.
The flowchart on the right shows visually what the flow of control looks like.
Let's see some simple examples of if statements.
In the first if statement,
we're checking if selection is equal to the character a.
If it is, then we execute the output statement.
If it is false we skip the output statement.
In the second example we're checking if num is greater than 10.
If it is, we execute the output statement.
In the last example,
we're using the and logical operator in the control expression.
In this case, we check to see if health is less than 100
and player healed is true.
If both these expressions are true,
then the entire control expression is true,
and we set the player's health to 100. Otherwise, we skip the statement.
What if you want to execute more than one statement if the condition is true.
Well, that's where block statements come in.
A block statement is a sequence of statements inside a block
which is made up of opening and closing curly brackets.
Notice the code on the left.
The two statements in the body of the if statement are inside curly braces.
If num is greater than 10,
then the block statement is executed which means that the increment
and the output statements are both executed.
The diagram on the right shows a visual representation of the control flow.
Let's talk a little more about blocks.
We've actually already seen a block statement.
Remember, the main function we've been using in all our programs so far,
it has an open curly at the beginning and a closed curly at the end.
This is a block statement and we had sequences of statements in the block statement.
Block statements introduce a new level of scope.
We'll talk about scope as we go through examples in this course.
But just like we declared variables in the main block,
we can declare variables in any block, even in a block that's inside an if statement.
Variables that we declare in a block are only visible to statements that are also in the same block.
This lets us modularize our code so we have better control of who sees what,
which makes writing and debugging our code easier.
Let's head over to the IDE and we'll see some if statements in action.
Okay. So I'm in the CodeLite IDE.
I'm in the section 9 workspace in the if statement project.
And what i want to do here is I want to create
a simple program where we'll create four different if statements. Let me show you what we'll do.
Let's say we've got a number line here and there's a minimum of 10
and the max is 100,
let's say, here. What I want to do is i want to ask the user to enter an integer
and I want to be able to check that the integer is greater than or equal to 10.
So that'll be one of the if statements.
I want to make sure that the integer they entered is less than
or equal to 100,that would be the second one.
The third if statement I want to write is I want to be sure that that
integer they enter is within that balance,
so between 10 and 100 including 10 and 100.
That'll be the third one. And then the fourth if statement we'll write is we'll check that boundary condition,
so we want to know whether it's actually equal to 10 or equal to 100.
So those are the four if statements we'll write, and we'll talk a little bit about
if statements as we go.
So first thing I want to do is i want to create some variables.
I need an integer, I'll just call it num
and I'll initialize it to 0. This is where I'm going to read the integer from the user into.
Then I need to model those boundaries, so I'll use constants for that.
I'll just say min and max. And we'll set min to 10,
and we'll set max to 100.
Okay. So that represents those boundaries.
So the first thing I'll do is I'll ask the user to enter
a number between
min and max.
Okay. Simple as that, and let me put a little
call on here so they can see what they're doing.
And then what we'll do is we'll read that into nom.
That's the only input we're going to be able to get in this program,
just a simple integer we'll get it from the user.
Enter a number between min and max. Let's test that. Make sure we didn't make any mistakes.
Enter a number between 10 and 100, exactly.
And I'm entering 10. I'm doing nothing right now with it.
And let me just get rid of that little extra space that I had there.
Okay. So now let's write our first if statement, that'll be number one here.
So I'm simply going to say if the number that the user entered
is greater than or equal to 10, which in this case is min
then all I really want to say is
there's my output statement I'm going to say num,
and I'll say is greater than min.
That's it. All right. So let's test that out.
So when I am going to type something greater than 10 now, I'm going to type in 20.
That if statement should fire. And I get 20 is greater than 10. Perfect.
Now if i type something in that is not greater than 10,
let's say, 5, I should see no output,
which is exactly what happens because that statement will be skipped.
Now suppose that I want to display something else,
I want to execute more than just that one statement.
This is where you need a block. So I'll create a curly here,
and I'll put a curly down here below the if.
That establishes a block. Notice that I'm indenting. I'm not writing all my code
all flush on the left.
If you indent, it's much easier to see the structure of the code.
And as you indent further and further and you nest things one within, the other
the structure of the code really jumps out at you, and it's very easy to follow.
So in this case, I want to
do a statement right up here since we're going to write four if statements. I'm going to say
let's do a slash and to give me a new line at the beginning,
and I'm just going to put some dashes and then I'm going to say if
statement one. I'll put some more dashes here at the end.
You'll see why I'm doing that in a little bit.
Okay. So if we run that now, we should see
both of those lines display. So I'll enter something greater than 10,
I'll put in there 30,
and those are both my lines. My if statement one line as well as the
30 is greater than 10. So again, really simple anytime you want to execute
more than one line of code or more than one statement,
you need to wrap them up in a block.
Now suppose that I also want to display how far away that is from min,
and what's the difference between the number that I entered and min.
Well, I could just subtract, right. It's pretty simple.
So we could create an integer here,and I'll just call it let me
create a little space here.
Okay. I'll call I have an integer. I'll just call it diff for the difference,
and I'll initialize that to the number the user typed in
minus the min, right.
Because I know it's greater than min, so that's going to be positive.
And then I'll just say cout
the number you entered
is,
and I'll use diff here. They might the difference amount.
And I know it's greater.
And I know it's greater than min.
That's it.
Now it's really important to understand a couple of things here.
One is this variable, I'm declaring that variable diff
inside the block. You can see the block right here. There's one curly.
There's the other curly.
So what that means is that, that variable
diff is only available to the statements
after I've declared it inside the block basically. So it's only this block.
That's it. Nothing else can see that variable. This is a good thing.
If that diff gets a really screwy value in there and it's really out of whack,
well then the only place it could have changed that is this block. So I know exactly where to look.
Okay. So let me clear a little bit of that,
and what I'll do now is let's run this
and see if this is correct.
So I'm going to enter a number between 10 and 20 and 100 and say 150.
I know that that is greater than 10 and I know that it's 40 away from 10.
So that's what I'm expecting. And there's my if statement one,
50 is greater than 10 and 50 is 40 greater than 10. Perfect.
If I again type something in like 5, I'll get no output,
which is perfect. That's exactly where I want to be.
So that takes care of that first if statement.
Now let's write the second if statement.
This one we're going to check to see if the number is less than or equal to max,
right. So it's the same idea. I'm just going to say if the number
is less than or equal to the max, that's the upper bound.
Then I want to display this c out statement. Let me just copy and paste that.
And I'll just change this to a 2. So that's my if statement 2.
And over here, I want to say that my num
is less than in this case.
Actually, it should be less than or equal to, right.
I should fix that up here because it should -- I know that it's greater than
or equal to since that's what I'm doing in my condition.
Let's fix that real quick. There we go. That should do it. In this case,
it's less than or equal to max.
Okay. And let's do the difference here as well.
Now here, if I say -- if I just do something like diff is
equal to max minus num,
which is the calculation I want to do but when I compile this you're going to get an error.
It's going to say diff was not declared in the scope because
diff is gone now. Diff was declared in this block.
That block is finished, so diff isn't even visible anymore.
So i need to create another if diff. So if i just say int
diff,
and I'll just initialize it to max minus num.
Similar to what i did above.
This creates a new copy of that integer diff that's only visible to this block here.
Now sure I could have just created that diff way up here
like I did with num and it would be visible to everybody. But I just wanted to show you how the scope rules work.
Okay. So there's my diff, and I'm going to say cout.
The number you entered is diff amount.
In this case, less than max.
In this case, less than
max.
And I'll finish that off. Okay. So let's run this, test this out.
All right. So let's type a number
like 80.
We expect
both those if statements to fire, right because 80 is greater than 10 and it's also less than 100,
less than or equal to 100. So there we go. Both if statements fire,
80 is greater than or equal to 10. 80 is 70 greater than 10.
And over here we're saying 80 is less than or equal to 100.
And it's 20 less than 100. So both those if statements fire.
But let's test this with other numbers here.
So let's say I type in 150.
In this case, only if statement one fires because 150 is greater than or equal to 10,
but it's not less than or equal to 100.
Okay. Same thing if I type in,
let's say, 5.
Only if statement two fires now.
Okay. So hopefully that makes sense. So now
the third if statement we want to write is the one that's right here.
It's going to check to make sure that that is within the bounds.
Okay. So in this case what we'll say is we'll say if
the number the user entered
is greater than or equal to the min
and the number the user entered
is less than or equal to the max.
Remember, you've got to do this here with num.
You've got to write that word in you can't just say less than we go to max.
You've got to say num less than or equal to max.
Again, I'll have my block statement here notice that when i type that beginning curly,
CodeLite finishes it off with the closed curly.
And let's copy this guy right here.
And we'll say that this is if statement three.
And in this case, we'll just say something like cout,
num
is in range let's say.
Okay. So we know that it's within that range.
There's no difference here to take care of we could do differences. But let's just do that.
What's interesting here is that statement three
will only execute
when statements 1 and 2 execute, right, because we're in range. We're greater than 10 and less than 100.
So let's write that down as well. We'll say cout.
This means statements one and two
must also be true
or must also display, in this case. Let's just say display.
Okay. Hopefully, that makes sense because you can see what's going on here.
If I entered a number that's in range somewhere in here,
then obviously this is going to be true and this is going to
be true. So we'll have all three of those statements displaying.
Okay. So we'll run this and test it we'll enter a number in the middle. Let's put in 50.
Notice all three if statements fire, which is exactly what we'd expect.
If we type in a number that's greater than 100,
only if statement one fires. And if we enter a number
that's less than 10, only if statement two fires.
Okay. So now the last one we'll do is we'll actually check to see if there it's right on those boundary
conditions. So we'll say if the number the user entered
is equal to the min
or the number the user entered is equal to the max.
Notice how you have to have that or in there.
The and would not work because it's never going to be equal to both.
So in this case, we just care if it's equal to one or the other.
In which case, we'll output statement four.
We'll change that to a four real quick, and I'll leave this code available for you guys to play with afterward
and mess around with all you like. And then what we'll say is cout
num is right on a boundary.
And what's neat about this one is that if this one fires then all four statements
must be fired. So again I'll say
that all four display.
And we'll give this a quick test.
Okay. So let's do one of the boundary conditions 10. There you go.
All four if statements fire because 10 obviously --
we're in the range, we're on the boundary,
we're greater than one side and we're less than the other. So we're good here.
And again if I just type a 5,
only that second if statement fires. So you can see some of the logic.
Now what's important to understand here, let me just clear the screen real quick.
What's important to understand here is a couple of things.
One is these are independent if statements. There are no elses in here.
We'll talk about else's in the next video.
So this statement will execute. So that condition is checked.
If this condition is true, we execute this code.
Then we come here and we check this condition.
So we're basically checking the condition for
all four of those if statements.
Okay. So hopefully that gives you some insight into if statements. They're pretty straightforward.
Remember, this condition we have in here
could be anything.
We could use relational operators like we're doing here.
We can use equality operators like we're doing down here.
We're comparing num and min. We could use logical operators like we're doing right here.
And we can use all kinds of combinations of these things as well.
Okay. So that finishes up off this video.
In the next video, what we'll do is we'll talk about the if else statement.