WEBVTT

00:00.490 --> 00:06.050
In this lecture I want to formalize how we make decisions in C++.

00:06.120 --> 00:12.690
We talked before about if statements in control flow and that's what we're going to talk more about

00:12.690 --> 00:13.420
today.

00:14.190 --> 00:23.290
Need some way of making decisions based on a condition and we use the if in otherwise for that in the

00:23.290 --> 00:24.750
first section.

00:24.760 --> 00:28.600
Now we want to go over how we do that in C++.

00:28.630 --> 00:32.640
So let's start with a an example here.

00:32.650 --> 00:37.310
So index sequel 0 y with zero

00:40.190 --> 00:46.790
in say you want to get two numbers from the user.

00:46.800 --> 00:49.760
So please enter two numbers.

00:50.970 --> 00:53.950
This is piece to me.

00:58.260 --> 01:00.840
And we'll get those two numbers.

01:00.980 --> 01:01.360
In

01:05.200 --> 01:14.400
and now you know what we want to do is see which which number is bigger than the other.

01:14.430 --> 01:23.560
So how we do that is we say if x is greater than Y

01:27.750 --> 01:33.330
then and notice the traces here see.

01:33.360 --> 01:34.220
Ouch.

01:37.280 --> 01:50.110
Do something strange in this a case one bigger number is Ingrid's X

01:53.860 --> 01:57.230
so knows how we construct an if statement.

01:57.340 --> 02:07.640
It always starts with if the left keyword so is parentheses around this what's called the condition

02:09.820 --> 02:12.890
and the in parentheses.

02:13.220 --> 02:14.670
So this is how you start.

02:14.710 --> 02:18.970
If statement say say F for the C and then you write the condition.

02:18.980 --> 02:22.700
In this case is X greater than Y.

02:22.850 --> 02:30.320
And this condition has to evaluate to TRUE or FALSE and that's why we've been learning the boolean logic

02:31.340 --> 02:35.430
and notice here we have the statement Plock.

02:35.450 --> 02:42.520
So remember in the first lecture of this section we went over these curly braces and what they mean.

02:42.770 --> 02:51.460
Well this means that if this is true of this value it's true then it will do this statement.

02:51.470 --> 02:58.680
BLOCK They all can have as many statements here as we want.

02:58.810 --> 03:02.040
OK we only have one in this case.

03:02.190 --> 03:07.950
So if this is true if x is greater than Y it'll do this case.

03:07.950 --> 03:12.500
If it's not and we used both before we used otherwise.

03:12.510 --> 03:21.760
But really in C++ it's it's else use keyword else.

03:21.860 --> 03:24.730
And again notice these braces.

03:24.800 --> 03:45.520
So Otherwise if it's not greater then why you do case to bigger number is why.

03:45.600 --> 03:54.300
So if this evaluates to false then it'll go here into this else.

03:54.310 --> 04:00.280
Case in the else case is really a catch all case.

04:01.080 --> 04:05.380
And you'll see why I say that in a minute.

04:05.620 --> 04:14.060
But if this is a false then it'll always just do this last case this L's case case to hear.

04:14.170 --> 04:23.990
So let's run this and see say 10 in three.

04:24.070 --> 04:24.980
So it does.

04:24.980 --> 04:27.940
Case 1 the bigger number is 10.

04:28.250 --> 04:30.040
So we got the two numbers.

04:30.110 --> 04:32.860
X was 10 in Y's three.

04:32.920 --> 04:35.730
Ten is of course bigger than three right.

04:35.760 --> 04:37.710
Ten is greater than three.

04:37.790 --> 04:45.770
So it did this first case and said see out Case 1 in the output she's won the bigger number is 10.

04:46.520 --> 04:47.130
OK.

04:47.410 --> 04:49.500
Let's run this again.

04:49.660 --> 04:59.220
Let's say three and ten so we can switch the order and it says case to the bigger number is 10.

04:59.360 --> 05:05.300
So that's why we have this case one case to tell which one of these cases it went to.

05:05.300 --> 05:09.830
So this is really what I mean by control flow it depends.

05:10.380 --> 05:23.890
Well different executions will get will get executed depending on the initial condition as the initial

05:23.890 --> 05:26.360
condition x is greater than Y.

05:26.590 --> 05:30.880
So it will execute different code depending on that condition.

05:32.720 --> 05:37.110
So of course this is kind of still not right.

05:37.300 --> 05:44.720
What if the two numbers are the same size 10 and 10 here does.

05:44.770 --> 05:52.200
Case two the bigger number is 10 because 10 is not greater than 10 so that they evaluate to false.

05:52.220 --> 06:00.710
So it kind of skipped this first case and then went down to this this last case here and just outputted

06:00.710 --> 06:06.910
that it's why but really these are equal so we kind of want to fix that.

06:06.910 --> 06:11.470
So how do we fix that is will say if else if

06:14.190 --> 06:19.380
X is less than Y right.

06:19.400 --> 06:23.450
So why greater than x then what print out why.

06:23.570 --> 06:26.820
So notice how we have this else here.

06:27.050 --> 06:35.400
This is really a way of changing multiple conditions to a single control.

06:35.540 --> 06:40.100
In this case the state control flow structure.

06:40.280 --> 06:48.470
So how this works is if this was false then it all skipped.

06:48.470 --> 06:52.560
This can this case and then evaluate this case.

06:52.570 --> 07:01.060
But if this was true it would never execute this second case if X was greater than why it won't execute

07:02.240 --> 07:03.370
this at all.

07:04.460 --> 07:08.540
So this wouldn't be run at all.

07:10.300 --> 07:20.440
So if x is greater than Y is false and if Y where X is less than Y is false.

07:21.270 --> 07:24.020
Then Case 3

07:29.550 --> 07:33.550
they must be equal or 8.

07:33.670 --> 07:38.260
So they would have to be equal in this case.

07:38.510 --> 07:41.730
And this is a 10 and 10.

07:42.090 --> 07:49.700
They must be equal right because 10 is not greater than 10 and 10 is not less than 10.

07:49.710 --> 07:52.080
So these are false.

07:52.410 --> 07:54.420
And then it went OK.

07:54.420 --> 08:01.890
So this catch all caught it and said OK they must be equal which they are.

08:03.630 --> 08:04.590
So if I did

08:09.840 --> 08:16.410
I did 5 3 5 5 and 10.

08:16.890 --> 08:19.320
So it did case to rape.

08:19.530 --> 08:23.850
So it's it said is five greater than 10.

08:23.850 --> 08:25.540
No this is false.

08:25.530 --> 08:32.170
So skip this case and I'll do is five less than 10.

08:32.280 --> 08:33.360
This is true.

08:33.720 --> 08:34.610
And then the output.

08:34.620 --> 08:38.790
This case to note is nowhere in the output.

08:38.800 --> 08:43.110
It's a case one case three and this is a control flow.

08:43.740 --> 08:49.940
So you can have as many else if extra conditions as you want.

08:49.940 --> 08:51.400
You can chain them all together.

08:53.760 --> 09:00.700
But it is you know always evaluate each one until you find the first one that is true.

09:01.230 --> 09:03.090
If none are true none that they can.

09:03.090 --> 09:05.260
Else if or if conditions it's true.

09:05.280 --> 09:11.040
It always goes to this catch all else.

09:11.090 --> 09:18.950
So in the next lecture I'll show you a better way of changing many many commands are many many conditions

09:18.950 --> 09:22.670
sorry together in a more efficient way.
