WEBVTT 00:00:06.990 --> 00:00:15.350 In this video we're going to learn that be for loops as we have seen in the javascript section for loops 00:00:15.360 --> 00:00:25.080 they are used to execute the same statements at many times with only one promiser which is going to 00:00:25.260 --> 00:00:28.800 change every time we run the statements. 00:00:28.800 --> 00:00:29.420 OK. 00:00:29.970 --> 00:00:32.230 So it starts with a simple example. 00:00:32.580 --> 00:00:36.840 So we get around the following statements. 00:00:37.350 --> 00:00:37.920 Echo 00:00:40.770 --> 00:00:42.620 a variable. 00:00:42.670 --> 00:00:46.080 It's going to be a number followed by a line break 00:00:50.230 --> 00:00:55.580 and we want to run this statement many times. 00:00:55.650 --> 00:00:57.570 OK for different values of y. 00:00:57.630 --> 00:01:08.450 So we want to start these statements with a value of pi equal to 1 until a value equal to 10. 00:01:08.460 --> 00:01:10.030 So to do that. 00:01:10.300 --> 00:01:15.230 But it's going to copy the statement at least for now. 00:01:15.690 --> 00:01:22.790 So we're going to use a for loop so the syntax is pretty similar to javascript's. 00:01:22.920 --> 00:01:31.680 OK so we're going to use the keyword for and then penances and then we're going to have curly brackets 00:01:31.940 --> 00:01:34.600 where are we going to write our statements. 00:01:35.490 --> 00:01:36.770 All right. 00:01:37.560 --> 00:01:47.540 So inside the pregnancies of four we get to define the conditions we want for our variable. 00:01:47.790 --> 00:01:57.780 So he is going to start and it's what they're going to have a semi-colon and then we write the condition 00:02:01.950 --> 00:02:05.040 which will set the final statements. 00:02:05.070 --> 00:02:08.660 So we want to be less than 10. 00:02:08.930 --> 00:02:10.930 OK so we're going to solve that one. 00:02:11.010 --> 00:02:16.630 We're going to continue until as far as I listened. 00:02:16.670 --> 00:02:21.660 So the last statement is going to be equal to 10. 00:02:22.250 --> 00:02:23.570 OK. 00:02:23.940 --> 00:02:29.960 And finally we're going to said the rule of incrementing boy. 00:02:30.000 --> 00:02:34.490 So we want every time to go up by 1. 00:02:34.620 --> 00:02:37.150 We're going to do it right plus plus. 00:02:37.230 --> 00:02:38.180 OK. 00:02:39.170 --> 00:02:40.070 So let's try this. 00:02:40.080 --> 00:02:47.290 If we save and refresh the page so we can see that we have printed numbers from 1 to 10. 00:02:47.460 --> 00:02:48.730 OK. 00:02:49.650 --> 00:02:53.190 All right so now let's move to another example. 00:02:53.400 --> 00:02:57.960 So this time we want to cycle through the elements of an array. 00:02:58.470 --> 00:02:59.400 OK. 00:02:59.400 --> 00:03:02.580 So this starts with a numeric array. 00:03:02.670 --> 00:03:07.400 So it creates a simple numeric array comics. 00:03:07.790 --> 00:03:08.840 OK. 00:03:08.960 --> 00:03:15.420 And you use the array class and let's have 00:03:18.180 --> 00:03:19.670 three values. 00:03:20.560 --> 00:03:21.690 OK. 00:03:21.900 --> 00:03:24.650 BMW. 00:03:25.350 --> 00:03:31.160 Audi and then Mercedes. 00:03:31.230 --> 00:03:31.700 OK. 00:03:31.800 --> 00:03:41.100 So what we want to do we want to run or execute a statements with a barometer which is going to take 00:03:41.100 --> 00:03:44.650 the value of the array elements each time. 00:03:44.750 --> 00:03:45.140 OK. 00:03:45.150 --> 00:03:50.070 So the problem of the beginning is going to be the first value that we can run the statements with the 00:03:50.070 --> 00:03:55.400 barometer equal to the second value and then the third value and so on. 00:03:55.620 --> 00:03:56.180 Okay. 00:03:56.370 --> 00:04:03.940 So now we were going to use a new keyword it's called for each. 00:04:04.410 --> 00:04:05.290 OK. 00:04:05.790 --> 00:04:16.160 So there's going to be followed by places where are we going to define our variable. 00:04:16.710 --> 00:04:23.390 And then we're going to have curly brackets to execute our correct or it's. 00:04:23.490 --> 00:04:33.390 So let's say the code you want to execute is we want to just write the value of our variable So let's 00:04:33.900 --> 00:04:41.790 say our variable is called value and I want to echo That's followed by a brick 00:04:44.500 --> 00:04:45.170 . 00:04:46.390 --> 00:04:59.120 OK so now inside the for each key word we are going to write the name of our array which is comics and 00:04:59.110 --> 00:05:07.790 then we we are going to write the key keyword is followed by the name we have chosen for our barometer 00:05:07.880 --> 00:05:09.670 which is value. 00:05:10.550 --> 00:05:12.260 OK. 00:05:13.000 --> 00:05:22.360 So you say in here is saying that for each element inside our array as. 00:05:23.290 --> 00:05:30.590 OK we're going to echo that's a variable value followed by a late break. 00:05:31.090 --> 00:05:39.640 So if we save and refresh the page now we can see that we echoed the first elements value followed by 00:05:39.650 --> 00:05:44.160 a second one and then said All right. 00:05:44.750 --> 00:05:51.120 Now we're going to go for another example but this time with this resistive array. 00:05:51.190 --> 00:05:56.080 So let's create this array first we're going to call it shopping basket. 00:05:57.250 --> 00:05:57.870 OK. 00:05:58.180 --> 00:06:08.200 And you use the array class and if you remember we need to use the following syntax so each element 00:06:08.200 --> 00:06:20.120 is going to have a key OK unfit followed by a value and then and between the key and the value we need 00:06:20.120 --> 00:06:21.830 to have an equal sign. 00:06:21.850 --> 00:06:22.660 I agree and. 00:06:22.880 --> 00:06:31.870 OK so I was the first as an example the first element is going to have a key a and a value yogurts. 00:06:32.000 --> 00:06:34.930 Let's create a couple of other 00:06:38.290 --> 00:06:41.490 elements with the keys and values. 00:06:41.590 --> 00:06:49.890 So the second one of the key B and if you. 00:06:50.410 --> 00:06:55.120 The last one see a value of x for example. 00:06:55.530 --> 00:07:03.310 OK so now we want to cycle through this array and executes 00:07:05.830 --> 00:07:13.890 a statements which is going to be as following so we want to echo the key. 00:07:14.320 --> 00:07:14.650 OK. 00:07:14.670 --> 00:07:21.980 For each element you want to echo the key followed by colon and then the value corresponding to that 00:07:21.970 --> 00:07:22.770 key. 00:07:23.380 --> 00:07:25.130 OK so what are we going to do. 00:07:25.130 --> 00:07:28.830 We're going to choose a variable for the key we're going to call it key. 00:07:29.240 --> 00:07:30.450 OK. 00:07:31.210 --> 00:07:33.730 So we're echoing the key followed by colon 00:07:37.820 --> 00:07:44.120 then followed by the value of the key for the value going to choose another promiser. 00:07:44.390 --> 00:07:45.930 We're going to call it value. 00:07:46.450 --> 00:07:46.790 OK. 00:07:46.840 --> 00:07:51.120 And it's going to be followed by a limerick. 00:07:53.470 --> 00:07:55.750 OK so this is our statement. 00:07:56.000 --> 00:08:04.940 Let's copy it and Control-C and we get the list for now so that we can create our for each statement 00:08:04.960 --> 00:08:04.990 . 00:08:05.000 --> 00:08:15.820 So this time we use a different syntax inside the foreach statements and then we are going to have curly 00:08:15.820 --> 00:08:19.360 brackets with our statements. 00:08:20.040 --> 00:08:20.570 OK. 00:08:20.950 --> 00:08:27.610 So we can non-rights the name of our array just as we did with the numeric array. 00:08:27.860 --> 00:08:28.300 OK. 00:08:28.300 --> 00:08:31.910 So our array this time is shopping muskets. 00:08:32.200 --> 00:08:40.490 Also we can raise the keyword as by this time we want to refer to both the key and the value. 00:08:40.720 --> 00:08:52.060 So all we're going to do is write a key followed by the the value with equal sign agreement was signed 00:08:52.060 --> 00:08:53.200 between them. 00:08:53.890 --> 00:08:59.800 OK so we're going to replace this with the variable key phrase does with the variable value. 00:09:00.000 --> 00:09:12.010 OK so in here we're going to have key and then we can have the variable value. 00:09:12.730 --> 00:09:13.280 OK. 00:09:13.420 --> 00:09:23.510 So now if we save and refresh the page we can see that we have printed keys for all of my colon followed 00:09:23.500 --> 00:09:25.840 by the corresponding values. 00:09:25.850 --> 00:09:29.380 Ok we started with the first one second one and then said several