WEBVTT 00:01.310 --> 00:04.840 In this medium we will be looking at control structures. 00:04.850 --> 00:11.260 Goal is a simple language so we will cover almost all of them. 00:11.420 --> 00:14.180 Control structures are the rules of our program. 00:14.180 --> 00:20.420 They analyze variables and decide the direction that the program will follow go is a simple language 00:20.570 --> 00:25.370 and like others it only has three control structures. 00:25.370 --> 00:31.070 Let's check them the first one is called for or for loop. 00:31.070 --> 00:37.630 It's a control step name that will execute some common code repeatedly until some conditions met. 00:37.730 --> 00:38.990 Let's see an example. 00:38.990 --> 00:49.050 Let's say we want to print the first 10 numbers so we can do it like this print 0 print one print 2 00:49.290 --> 00:50.180 and so on right. 00:50.580 --> 00:52.460 But it would be very smart. 00:52.650 --> 01:02.730 So in this cases when I want to use a for loop to use it the way to to express this loop is writing 01:02.730 --> 01:09.340 for initially and then those three sections the first one is the initialization. 01:09.420 --> 01:13.490 So let's say for E equals to zero. 01:13.500 --> 01:22.050 This will be our initial state the next stage will be the condition it reads while. 01:22.080 --> 01:32.130 It's less than let's say 10 and the after class which is gonna be increase e by one eye plus plus is 01:32.130 --> 01:38.660 the same the eye equals to eye plus one before shorter we will write it. 01:38.880 --> 01:40.380 Plus plus. 01:40.380 --> 01:52.210 So in this case what we're saying is I start with the value of 0 you will increase by 1 in every iteration 01:52.210 --> 01:53.090 of this loop. 01:53.410 --> 01:59.980 Its value until this condition is not met which is I is smaller than 10. 02:00.640 --> 02:05.440 So what are we going to do inside this loop is to print the value of ie. 02:06.310 --> 02:14.680 So there is gonna iterate I starting from zero and then in a smaller number than 10 which is nine. 02:14.740 --> 02:18.900 So to better understand this let me save it and executed. 02:19.070 --> 02:21.970 So what we see now in that value. 02:21.990 --> 02:27.130 I start with zero then gold one two three. 02:27.240 --> 02:27.840 Blah blah blah. 02:27.930 --> 02:28.530 Until nine. 02:29.400 --> 02:33.300 So this is the main use case of the for loop. 02:33.300 --> 02:37.390 It basically iterate until some condition is met. 02:37.800 --> 02:38.880 Doing something else. 02:38.910 --> 02:41.500 In this case implementing the value of pi. 02:41.660 --> 02:49.080 Now the next control structure is the if statement if it's a completely lost control structure were 02:49.170 --> 02:54.920 some condition is met once from some condition is met he will execute some sunblock. 02:55.680 --> 02:59.820 So let's expand this example by doing OK. 03:00.570 --> 03:04.870 Let's say we just want to print the numbers bigger than 5. 03:04.950 --> 03:05.440 Right. 03:05.790 --> 03:13.350 So we know I is gonna start from zero will end in nine but we just want to print the volume. 03:14.190 --> 03:19.340 When the condition AI is bigger than 5 is met. 03:19.420 --> 03:21.720 So to do it you just have to write. 03:21.850 --> 03:30.390 If the condition which is AI bigger than 5 in brackets right. 03:30.720 --> 03:37.080 What's gonna happen here is I'm just going to be printing AI when it's bigger than 5. 03:37.440 --> 03:45.360 But the loops and when it's smaller than 10 so we should print 6 7 8 and 9. 03:45.810 --> 03:48.420 Let's verify that works. 03:48.840 --> 03:49.180 Yeah. 03:49.230 --> 03:49.770 And it does. 03:49.770 --> 03:52.760 Right now I can do the upside. 03:52.920 --> 04:00.070 Let's say when I is smaller than fi printed and now I have from 0 to 4. 04:00.080 --> 04:03.930 Now there's what I can do is to define two conditions. 04:03.930 --> 04:10.200 So if the condition is met I will print the number in this case but it is not met. 04:10.230 --> 04:15.000 I can do something else and that's where the LS keyword will help me. 04:15.000 --> 04:26.390 So I'm gonna say here is if I use this more than 5 printed or otherwise just print let's say some text 04:26.570 --> 04:31.140 like I can't print why. 04:31.880 --> 04:35.300 So let's run this and see what happens from 0 to 4. 04:35.300 --> 04:44.660 It prints I but after that on every duration that if statement result these two falls so it falls into 04:44.660 --> 04:48.610 the else statement which is I can't print I read. 04:48.680 --> 04:51.430 Now let's go to a more useful case. 04:51.590 --> 04:57.120 Let's say you want to print the even numbers instead of doing this. 04:57.140 --> 05:03.050 I'm gonna use this operator which is the model's story. 05:03.170 --> 05:05.200 Is everything okay. 05:05.470 --> 05:11.960 So this is operator is gonna return zero when the number is even. 05:12.040 --> 05:17.380 So let's say this is just bringing the numbers that even numbers. 05:17.400 --> 05:17.720 Okay. 05:17.920 --> 05:25.870 Now a better way to express if conditions are not better but another way to express conditionals that 05:25.870 --> 05:29.250 can be easier to read when our conditions grow too much. 05:29.260 --> 05:30.250 It's called switch. 05:30.670 --> 05:32.640 So let's see what switch does. 05:32.650 --> 05:38.440 It's pretty similar to Eve but again when our conditions grow it's way easier to read. 05:38.440 --> 05:44.960 So let's go to some other example let's define our price function which is. 05:44.960 --> 05:49.220 Take an item which is a string and it's gonna return its price. 05:49.220 --> 05:50.570 Price is of course a number. 05:50.570 --> 05:53.170 So it's an end it's a typo. 05:53.170 --> 05:53.500 Okay. 05:53.600 --> 05:59.830 So let's say I have a list of items where I want to return the price I can use an if statement. 05:59.960 --> 06:04.340 But let's use a switch statement to see how it will look like. 06:04.340 --> 06:08.980 So it is going to be used with a switch keyword is common in very other languages. 06:09.020 --> 06:18.050 So let's switch the item and see if it's an apple for example with our return let's say 10 Apple cost 06:18.230 --> 06:21.970 if it's an orange price will be why. 06:22.330 --> 06:24.940 If it's a carrot let's say is cheaper. 06:24.950 --> 06:25.770 So it's fine. 06:26.180 --> 06:33.630 So if you can see here what it is doing is if this condition is met then returned its value. 06:33.650 --> 06:37.070 Else if this condition is met return is value. 06:37.070 --> 06:38.050 And so on. 06:38.050 --> 06:40.670 Now let's call the function to see if this really works. 06:40.850 --> 06:43.850 So let's do price Apple right. 06:44.310 --> 06:44.690 OK. 06:45.140 --> 06:52.580 And now it's failing that it is finding failing because if not none of these conditions is met I should 06:52.580 --> 06:55.490 return something but I'm not returning anything. 06:55.490 --> 06:57.720 So there's two ways to think that I can do here. 06:58.100 --> 07:01.760 I can return some default value which is going to be zero. 07:02.000 --> 07:03.570 And this is gonna work. 07:03.680 --> 07:06.410 So if I run this I have the price. 07:06.770 --> 07:07.720 So let me print it. 07:08.030 --> 07:10.670 So you can see it on the screen and it's fine. 07:10.670 --> 07:12.420 The price of Apple is 10. 07:12.560 --> 07:15.830 The price of carrot should be five. 07:16.190 --> 07:20.570 But the price of a laptop is not defined right. 07:20.990 --> 07:22.020 So if you return zero. 07:22.430 --> 07:23.690 Okay that's fine. 07:23.690 --> 07:30.260 Now there is another way I can do this which is using the default keyword and the default Cuba is basically 07:30.350 --> 07:35.950 if not the condition before his math regional 0 is the same effect. 07:36.050 --> 07:39.850 There is probably an hour and expressive way to say it. 07:39.870 --> 07:44.170 Now let's say you have two items which has the same price. 07:44.690 --> 07:47.250 Let's say apple costs 10. 07:47.300 --> 07:50.480 And also tomatoes has price. 07:50.720 --> 07:53.810 So I can change these by tomatoes. 07:55.720 --> 07:58.060 And it sure says 10 as well. 07:58.300 --> 07:58.690 Okay. 07:59.050 --> 08:09.970 So what was learned on this episode is that we have three control statement for Eve and switches and 08:09.970 --> 08:14.590 they are super useful for controlling our program and makes the decision we want. 08:15.490 --> 08:16.530 So that's it for today.