In this lecture I want to formalize how we make decisions in C++.
We talked before about if statements in control flow and that's what we're going to talk more about
today.
Need some way of making decisions based on a condition and we use the if in otherwise for that in the
first section.
Now we want to go over how we do that in C++.
So let's start with a an example here.
So index sequel 0 y with zero
in say you want to get two numbers from the user.
So please enter two numbers.
This is piece to me.
And we'll get those two numbers.
In
and now you know what we want to do is see which which number is bigger than the other.
So how we do that is we say if x is greater than Y
then and notice the traces here see.
Ouch.
Do something strange in this a case one bigger number is Ingrid's X
so knows how we construct an if statement.
It always starts with if the left keyword so is parentheses around this what's called the condition
and the in parentheses.
So this is how you start.
If statement say say F for the C and then you write the condition.
In this case is X greater than Y.
And this condition has to evaluate to TRUE or FALSE and that's why we've been learning the boolean logic
and notice here we have the statement Plock.
So remember in the first lecture of this section we went over these curly braces and what they mean.
Well this means that if this is true of this value it's true then it will do this statement.
BLOCK They all can have as many statements here as we want.
OK we only have one in this case.
So if this is true if x is greater than Y it'll do this case.
If it's not and we used both before we used otherwise.
But really in C++ it's it's else use keyword else.
And again notice these braces.
So Otherwise if it's not greater then why you do case to bigger number is why.
So if this evaluates to false then it'll go here into this else.
Case in the else case is really a catch all case.
And you'll see why I say that in a minute.
But if this is a false then it'll always just do this last case this L's case case to hear.
So let's run this and see say 10 in three.
So it does.
Case 1 the bigger number is 10.
So we got the two numbers.
X was 10 in Y's three.
Ten is of course bigger than three right.
Ten is greater than three.
So it did this first case and said see out Case 1 in the output she's won the bigger number is 10.
OK.
Let's run this again.
Let's say three and ten so we can switch the order and it says case to the bigger number is 10.
So that's why we have this case one case to tell which one of these cases it went to.
So this is really what I mean by control flow it depends.
Well different executions will get will get executed depending on the initial condition as the initial
condition x is greater than Y.
So it will execute different code depending on that condition.
So of course this is kind of still not right.
What if the two numbers are the same size 10 and 10 here does.
Case two the bigger number is 10 because 10 is not greater than 10 so that they evaluate to false.
So it kind of skipped this first case and then went down to this this last case here and just outputted
that it's why but really these are equal so we kind of want to fix that.
So how do we fix that is will say if else if
X is less than Y right.
So why greater than x then what print out why.
So notice how we have this else here.
This is really a way of changing multiple conditions to a single control.
In this case the state control flow structure.
So how this works is if this was false then it all skipped.
This can this case and then evaluate this case.
But if this was true it would never execute this second case if X was greater than why it won't execute
this at all.
So this wouldn't be run at all.
So if x is greater than Y is false and if Y where X is less than Y is false.
Then Case 3
they must be equal or 8.
So they would have to be equal in this case.
And this is a 10 and 10.
They must be equal right because 10 is not greater than 10 and 10 is not less than 10.
So these are false.
And then it went OK.
So this catch all caught it and said OK they must be equal which they are.
So if I did
I did 5 3 5 5 and 10.
So it did case to rape.
So it's it said is five greater than 10.
No this is false.
So skip this case and I'll do is five less than 10.
This is true.
And then the output.
This case to note is nowhere in the output.
It's a case one case three and this is a control flow.
So you can have as many else if extra conditions as you want.
You can chain them all together.
But it is you know always evaluate each one until you find the first one that is true.
If none are true none that they can.
Else if or if conditions it's true.
It always goes to this catch all else.
So in the next lecture I'll show you a better way of changing many many commands are many many conditions
sorry together in a more efficient way.