WEBVTT 0 00:02.720 --> 00:08.060 Welcome, In this lecture, I'm going to talk about the "for" statement. In the first step 1 00:08.090 --> 00:13.880 You're going to learn about the four statements which allows us to repeat a block of statements. loops 2 00:13.880 --> 00:14.530 statement in 3 00:14.540 --> 00:16.170 Go is very simple. 4 00:16.340 --> 00:20.480 There is only one loop statement in Go unlike other languages. 5 00:20.480 --> 00:23.670 There are no "while" or "until" statements in Go. 6 00:23.990 --> 00:30.700 However you can create them using only the "for" statement. In the second step 7 00:30.770 --> 00:36.030 you are gonna learn how to use a loop inside another loop and the last step you're going to learn 8 00:36.030 --> 00:39.260 about the four range clause. 9 00:39.290 --> 00:40.040 All right! 10 00:40.280 --> 00:41.840 Let's start with the first step. 11 00:44.460 --> 00:44.960 OK. 12 00:45.090 --> 00:48.560 Now I'm going to talk about the basics of the "for" statement 13 00:49.140 --> 00:52.720 Let's first take a look at why you may need loops. 14 00:52.740 --> 00:54.720 Now I'm gonna show you an example code for that. 15 00:59.230 --> 01:04.330 ok in this very simple program I am going to find out the the sum of five numbers. 16 01:04.460 --> 01:11.980 OK so let's first declare sum variable here. var some int 17 01:12.010 --> 01:17.890 Now lets add the numbers beginning from 1 to 5 like this. 18 01:22.980 --> 01:24.460 All right ready. 19 01:24.570 --> 01:24.970 lets print them 20 01:27.720 --> 01:28.440 ok, let me run it 21 01:28.540 --> 01:33.500 now, it prints 15. correct! 22 01:33.520 --> 01:37.560 Now imagine that these numbers goes to 1000. 23 01:37.630 --> 01:44.870 It would be a waste of time to add these numbers by manually typing that as I said at the beginning of 24 01:44.870 --> 01:50.600 the section that we humans are very bad at doing those sort of things right. 25 01:50.990 --> 01:55.560 So that's why we need loops. Let's refactor this code to a loop. 26 01:55.560 --> 02:05.690 now, let me type look at numbers between 1 and 1000 like thi. for short declare. 27 02:05.720 --> 02:14.510 Start with 1 and loop to 1000 . increments to 28 02:14.590 --> 02:17.150 i variable. OK! 29 02:17.350 --> 02:23.440 then inside the loop, I'm going to assign the current i variable to sum variable using an assignment 30 02:23.440 --> 02:27.550 operation like this. 31 02:27.560 --> 02:28.390 All right, done! 32 02:28.580 --> 02:29.410 Let me run it. 33 02:30.680 --> 02:35.410 As you can see the computer has very quickly added the numbers without getting tired at all. 34 02:36.420 --> 02:41.340 That's what they do. I think now it's clear why we need the loops. 35 02:41.490 --> 02:46.800 If you have a programming background already I know this was a very simple example. but I needed to do 36 02:46.800 --> 02:48.720 this for also beginners. 37 02:48.720 --> 02:52.200 Ok now I'm going to explain each part of this "for" statements. 38 02:56.080 --> 03:02.200 This is the initialization statement. This init statement is just a simple statement as you've seen it 39 03:02.200 --> 03:03.240 before. 40 03:03.250 --> 03:05.940 This way I can use a short declaration here. 41 03:07.010 --> 03:13.910 As you can see here it's usually used for declaring variables. Any declared variables here can only be used 42 03:13.970 --> 03:21.840 inside these for loop. so they won't be visible outside of the for statement. Scoping rules still apply 43 03:23.510 --> 03:33.380 this statement will only be run once. Alright! that's all about it. Let's continue. This part is the condition expression 44 03:33.380 --> 03:34.550 of the for loop. 45 03:34.850 --> 03:40.670 It should be a bool expression only. As long as this condition yields true. 46 03:40.670 --> 03:42.090 the loop will continue. 47 03:43.060 --> 03:52.140 For example here, as long as the i variable is less than or equal to 5, this loop will continue. before the 48 03:52.140 --> 03:53.250 loop begins. 49 03:53.270 --> 04:00.330 Go will first check this condition and if it yields true the loop will start and after the first step of 50 04:00.330 --> 04:04.660 the loop ends go will check it again. if it yields true 51 04:04.920 --> 04:06.810 the loop will continue. 52 04:06.870 --> 04:08.170 when It becomes false. 53 04:08.220 --> 04:09.330 the loop will end 54 04:12.150 --> 04:12.600 OK. 55 04:12.640 --> 04:17.180 This is the post statement of the for loop. like the init statement, 56 04:17.200 --> 04:18.650 this is a simple statement. 57 04:18.790 --> 04:21.830 So that's why I can use an inc-dec statement here. 58 04:21.850 --> 04:24.660 This statement will be run after each step of the loop. 59 04:24.760 --> 04:30.490 So Go will first run the statements inside the for statements block and after that it will run this 60 04:30.490 --> 04:31.220 statement. 61 04:31.280 --> 04:37.810 I've just used an inc - dec statement here for incrementing the "i" variable so i variable will keep incrementing 62 04:37.900 --> 04:39.190 until the loop ends. 63 04:42.320 --> 04:47.130 these highlighted parts are used for separating the parts of the for statements. 64 04:47.390 --> 04:48.860 their meaning is clear. 65 04:48.860 --> 04:51.230 so lets continue. 66 04:51.420 --> 04:57.310 This is the block of the for loop. as you can see the i variable is available here. 67 04:57.420 --> 05:03.870 It's within the scope of the for loop right? Any statements inside this block will be repeated until 68 05:03.870 --> 05:07.960 the i variable becomes greater than 5 and that's all 69 05:11.280 --> 05:11.680 OK. 70 05:11.680 --> 05:12.580 Let me summarize 71 05:15.400 --> 05:16.080 it first 72 05:16.090 --> 05:22.840 short declares the i variable and assigns one to it in the initialization only once. then in its condition 73 05:22.840 --> 05:23.500 expression. 74 05:23.500 --> 05:29.270 It checks whether the i variable is less than or equal to 5. at the beginning it's zero. 75 05:29.290 --> 05:36.660 so the loop starts then Go runs the for statements block which in turn adds the i variable to the sum 76 05:36.690 --> 05:37.130 variable 77 05:38.220 --> 05:45.250 then it executes the post statement which in turn increments the i variable and it repeats this as long 78 05:45.310 --> 05:49.070 as the i variable becomes greater than 5. 79 05:49.170 --> 05:55.610 By the way in Go, all these parts can be removed except the block. In the next lecture 80 05:55.620 --> 05:58.120 I'm going to show you how to do that. 81 05:58.120 --> 05:59.010 That's all for now. 82 05:59.010 --> 06:00.030 Thank you for watching. 83 06:00.030 --> 06:01.360 See in the next lecture. Bye Bye!