WEBVTT

00:01.100 --> 00:04.610
In this lecture you will learn about the conditional statements.

00:04.610 --> 00:09.440
So conditional statements allows you to make decisions in your programs.

00:09.440 --> 00:16.160
So these decisions rest on boolean expressions so which evaluate a true or false.

00:16.160 --> 00:23.390
So for example, you can compare some operators such as greater than or not equal to world to build

00:23.390 --> 00:24.950
a boolean expression.

00:24.950 --> 00:32.510
So some basic comparison operators, let's create an here for example, integer, let's create an integer

00:32.510 --> 00:39.050
value variable named X here and assign it to zero and 42 equals here.

00:39.050 --> 00:44.000
I will I will write all of these quotes here and we'll explain later here.

00:44.000 --> 00:49.880
42 not equals to zero.

00:51.980 --> 00:56.930
Here and the 100 greater than X here.

00:57.650 --> 00:58.670
One, two, three.

00:58.700 --> 01:08.900
Greater than or equal to X here and minus ten less than X here.

01:10.500 --> 01:14.550
And -99 less than or equal to X here.

01:15.540 --> 01:19.480
So let let me write this here.

01:19.500 --> 01:21.480
This is the equality operator.

01:22.380 --> 01:25.110
It will return boolean or false.

01:25.350 --> 01:27.450
This is the inequality operator.

01:27.480 --> 01:29.160
Inequality.

01:30.210 --> 01:32.730
And this is here.

01:32.850 --> 01:35.730
Actually, let me use the tabs here.

01:38.310 --> 01:38.810
Okay.

01:38.820 --> 01:40.350
One, two.

01:41.200 --> 01:41.740
Three.

01:42.790 --> 01:44.590
This is a unique operator here.

01:44.590 --> 01:47.710
This is greater than and.

01:51.330 --> 01:54.930
This is less greater than.

01:55.170 --> 01:59.760
Greater than or equal to.

02:01.240 --> 02:01.600
Here.

02:01.600 --> 02:12.310
This is less than and this is less than or equal to operator.

02:14.090 --> 02:14.660
Here.

02:15.110 --> 02:18.920
So this program produces no output.

02:19.010 --> 02:22.090
Let's compile it and show what it will produce.

02:22.100 --> 02:25.250
This will return zero because we are not apparent.

02:25.370 --> 02:29.930
We are printing nothing here and using no variables here.

02:29.930 --> 02:40.130
As you can see, Sea Lion automatically assign them, assign not the note them as unused variables.

02:40.130 --> 02:46.850
So while the program doesn't produce any output compiling, it helps to verify that you are written

02:46.850 --> 02:49.660
as valid plus plus code.

02:49.730 --> 02:59.840
So to generate more interesting programs, you use a conditional statements like if here so an if statement

02:59.840 --> 03:05.060
contains a boolean expression and one or more nested statements.

03:05.060 --> 03:09.020
So depending on whether the boolean value is a true or false.

03:09.020 --> 03:13.290
So the program decides which nested statement to execute.

03:13.290 --> 03:16.080
So there are several forms of if statement.

03:16.080 --> 03:26.220
But basic logic here is you use the boolean expression question and then statement.

03:27.580 --> 03:28.660
Your statement.

03:29.810 --> 03:32.060
Or, for example, statement here.

03:32.510 --> 03:35.660
So as Boolean expression, let's create a bool type here.

03:35.690 --> 03:39.200
Bool boolean expression.

03:40.570 --> 03:41.260
Test.

03:41.680 --> 03:42.300
Test here.

03:42.910 --> 03:44.080
Assign it true.

03:44.380 --> 03:48.610
So if this boolean expression is true.

03:51.830 --> 03:53.180
Boolean expression.

03:53.180 --> 03:59.720
So if this boolean expression is true, then our statement will be executed.

03:59.720 --> 04:01.160
So as a Boolean expression.

04:01.160 --> 04:02.690
So let's create a bool here.

04:02.720 --> 04:07.310
A You can write this as a Boolean expression here.

04:07.310 --> 04:08.750
So let's put it here.

04:08.750 --> 04:12.710
And as you can see here, we got no error here.

04:12.710 --> 04:23.510
So if here print F or C out STD, um, or let's just use print f.

04:27.120 --> 04:27.570
We're.

04:29.570 --> 04:32.900
If a print f.

04:34.320 --> 04:36.630
We have to enter the variable here.

04:36.660 --> 04:37.250
Okay.

04:37.260 --> 04:47.490
We use this studio here because this printf contains this printf has this studio has the printf function

04:47.490 --> 04:48.000
in it.

04:48.090 --> 04:55.150
So this, as you know, printf is not default, just system method in C plus plus here.

04:55.170 --> 04:59.430
So let's create an executed or.

05:00.370 --> 05:01.030
Yeah.

05:01.090 --> 05:03.160
X here.

05:03.430 --> 05:05.650
And let's run this code.

05:05.710 --> 05:14.160
As you can see, we got no output here because our boolean expression is false, as you can see here.

05:14.170 --> 05:21.700
And if we put this if 42 equals 42, this will evaluate True.

05:21.730 --> 05:25.330
That's why our code will be executed.

05:25.630 --> 05:29.770
Here we have another another if statement.

05:29.770 --> 05:34.720
You can write this like compound statement because sometimes you will want to a group of statements

05:34.720 --> 05:36.970
to run rather than a single statement.

05:36.970 --> 05:40.840
If you do single statement, you can use this as follows.

05:40.840 --> 05:44.980
So such a group is called the compound statement.

05:44.980 --> 05:51.580
So to declare the compound statement, simply wrap the group of statements in a braces here like this

05:51.580 --> 05:57.210
so you can use compound statements within within the if statements as follows here.

05:57.220 --> 06:01.520
So let's create an A here or let's create another Boolean expression.

06:01.520 --> 06:05.630
So if 100 is Boolean, be here.

06:05.630 --> 06:13.130
If 100 is greater than it, 100 is greater than X here.

06:13.460 --> 06:14.090
Here.

06:20.260 --> 06:22.600
Let's make it look nice.

06:24.500 --> 06:25.070
Here.

06:30.170 --> 06:31.730
Here and here.

06:34.110 --> 06:34.980
So.

06:36.210 --> 06:37.140
Uh, okay.

06:37.410 --> 06:50.220
So if B, our B variable is if this expression will return the true, then our if function if statement

06:50.520 --> 06:52.290
if statement will be executed.

06:52.290 --> 06:54.120
If not, this is not a function.

06:54.120 --> 06:55.350
This is a statement here.

06:56.290 --> 06:59.860
So let's create let's give this as an example.

06:59.890 --> 07:01.690
A, if B.

07:03.470 --> 07:05.630
Is true, evaluates true.

07:05.660 --> 07:08.300
Then print F this year for example.

07:08.810 --> 07:09.650
Code.

07:10.960 --> 07:12.010
Executed.

07:13.830 --> 07:14.700
Printf.

07:15.420 --> 07:16.320
Executed.

07:17.850 --> 07:18.720
But to.

07:20.670 --> 07:24.600
Here code one and executed code two.

07:25.260 --> 07:25.890
Here.

07:25.890 --> 07:27.300
And use this.

07:27.300 --> 07:29.400
So let's use this.

07:29.400 --> 07:32.220
And as you can see here, let's actually add new line.

07:32.220 --> 07:34.110
So make it look nice.

07:34.990 --> 07:35.590
Here.

07:35.590 --> 07:36.880
So executed.

07:36.880 --> 07:37.750
Code one.

07:37.780 --> 07:39.520
Executed Code two.

07:39.550 --> 07:43.170
So in this case, our both this statement is executed.

07:43.180 --> 07:46.810
So actually, let's make this comment two.

07:48.400 --> 07:54.880
So if the comment is, as you know, the compiler will not compile this code and use this as a comment,

07:54.880 --> 07:55.880
just a text here.

07:55.900 --> 08:04.120
As you can see, the both of our functions, both of our expressions and quotes is executed in this

08:04.120 --> 08:05.780
if statement.

08:05.800 --> 08:15.760
So if we if this value is true or we can use this and directly give this as an here, if 100 is greater

08:15.760 --> 08:18.540
than X, then execute our code.

08:18.550 --> 08:20.470
As you can see, we get the same output.

08:20.470 --> 08:29.080
So if we change to use less than operator here, this function, this code will not x this if statement

08:29.080 --> 08:30.280
will not execute.

08:30.310 --> 08:33.700
As you can see here, the x equals zero.

08:33.700 --> 08:37.960
So our 100 is not less than zero.

08:37.960 --> 08:43.840
And this if statement will not execute in this time.

08:43.840 --> 08:49.700
So you can elaborate the if statement using the else if and else statements here.

08:49.700 --> 08:55.760
So these optional additions allows you to describe more complicated branching behavior.

08:55.760 --> 08:58.850
So let's create another example here.

09:00.740 --> 09:04.310
If so, make it here.

09:04.310 --> 09:12.080
And if our boolean expression is true, then use the statement.

09:12.740 --> 09:13.760
Print f.

09:16.720 --> 09:18.520
My statement.

09:20.190 --> 09:23.130
One and New Line.

09:25.270 --> 09:28.000
And here else if.

09:29.300 --> 09:31.880
Our my statement.

09:31.910 --> 09:34.280
My statement is true.

09:34.520 --> 09:35.510
My statement.

09:35.540 --> 09:43.130
This is a this is a Boolean expression we have to give here if false or let's actually use the create

09:43.130 --> 09:44.860
a new variable integer.

09:44.870 --> 09:50.150
My var variable here is 100 and integer.

09:50.150 --> 09:52.820
My my.

09:54.110 --> 10:02.210
My car here is under and my van here is 50 and integer my.

10:03.960 --> 10:07.370
My phone is, for example, ten.

10:07.380 --> 10:09.480
This is just an example variable names.

10:09.480 --> 10:11.970
So if our my car.

10:13.350 --> 10:16.050
Is greater than zero.

10:18.470 --> 10:20.210
But actually, we don't need these variables here.

10:21.080 --> 10:27.920
Then evaluate my car greater than greater than 100.

10:29.060 --> 10:29.650
Here.

10:29.660 --> 10:34.640
And then if this statement is not executed, use the.

10:37.030 --> 10:44.020
My car and give the second statement if my car equals greater than.

10:44.520 --> 10:51.340
Uh, actually, let's make this 50 and my car is greater than 50.

10:51.460 --> 11:01.000
And then you give this my car greater than 100.

11:01.030 --> 11:01.690
Here.

11:04.610 --> 11:05.180
Hundred.

11:14.450 --> 11:16.310
And then else.

11:16.310 --> 11:19.350
And we else we don't have to give condition here.

11:19.370 --> 11:31.460
Then if these conditions will not execute, then use this else condition here and we're going to write

11:31.460 --> 11:32.210
it here.

11:33.290 --> 11:41.090
We don't know about what or just evaluates.

11:42.450 --> 11:44.280
S statement here.

11:48.050 --> 12:04.460
If and else if statements does not executed here and our comment is executed here and use this here.

12:04.460 --> 12:11.270
So let's run this program and executed code my car greater than 50 here.

12:11.300 --> 12:17.510
As you can see here, our car, my car is equals to zero and my car is equals to 100.

12:17.510 --> 12:24.110
And that's why this else if command is not executed and let's create an let's assign my car 101.

12:24.350 --> 12:29.510
And as you can see here, my car is greater than 50 here.

12:30.720 --> 12:37.470
So if we do like delete the URLs here and use this.

12:37.470 --> 12:44.220
And as you can see, both of our expression expressions will be executed here.

12:45.370 --> 12:46.570
We'll make it nice.

12:46.570 --> 12:49.180
So my car greater than 50.

12:49.210 --> 12:52.400
My car greater than 100.

12:52.420 --> 12:57.070
So the first boolean expression here is evaluated here.

12:57.100 --> 12:59.190
Actually, let's make it evaluated.

12:59.200 --> 13:07.630
So if boolean expression one is true and then statement one is evaluated and the if statement stops

13:07.630 --> 13:15.610
executed and if boolean expression one is false, then boolean expression two is evaluated.

13:15.610 --> 13:16.390
So.

13:17.440 --> 13:25.260
If true, this statement to hear if Boolean expression two is true, then the statement two will be

13:25.260 --> 13:26.030
executed.

13:26.040 --> 13:27.100
Otherwise.

13:27.120 --> 13:29.940
Statement three will be executed.

13:29.940 --> 13:34.380
So if you make it like one, then our.

13:35.500 --> 13:41.110
Executed code two here actually if integer mcar.

13:41.110 --> 13:42.470
And else.

13:42.490 --> 13:43.570
Else if.

13:46.920 --> 13:47.400
Here.

13:47.610 --> 13:55.710
So you can include any number of else if classes or omit them entirely.

13:55.710 --> 14:03.600
So as with the initial if statement, the boolean expression or for each else if class is evaluated

14:03.600 --> 14:04.290
in order.

14:04.290 --> 14:12.510
So when one of these boolean expression evaluates to true evaluation stops and corresponding statement

14:12.510 --> 14:13.140
executes.

14:13.140 --> 14:24.240
So if no else if evaluates to true the else here, statement three will always execute.

14:47.030 --> 14:50.150
So now I want to write some code here.

14:50.150 --> 14:59.800
So integer create an integer here or let's value make my card variable zero int here.

14:59.810 --> 15:14.750
Then if our my card greater than zero then here print f my car, my car greater than zero here or here.

15:15.410 --> 15:18.920
And else if here.

15:22.520 --> 15:28.300
If my car less than zero, then print f.

15:31.120 --> 15:32.880
Print F negative.

15:32.890 --> 15:34.510
My car.

15:34.930 --> 15:35.950
My car.

15:36.100 --> 15:37.570
Less than zero.

15:37.750 --> 15:39.880
And else statement here.

15:43.190 --> 15:50.570
My car print f my car equals to zero.

15:51.680 --> 15:55.460
So now we can run this program and see what happens.

15:55.490 --> 15:56.660
Actually, let me.

15:59.540 --> 16:02.660
So as you can see in this case, my card is zero.

16:02.660 --> 16:09.670
So if we assign and this if statement is executed, as you can see here.

16:09.680 --> 16:14.030
So if we make it more my card to.

16:15.250 --> 16:22.750
Then my car greater than zero will execute as well if we make it minus one.

16:24.000 --> 16:26.550
Then my card less than zero.

16:26.550 --> 16:26.970
Here.

16:26.970 --> 16:28.650
This statement is executed.

16:28.680 --> 16:36.900
If you make it zero again, then the else statement my car equals to zero will be executed.

16:36.900 --> 16:37.920
And that's it.

16:37.920 --> 16:41.460
About the C plus plus conditional statements here.
