WEBVTT 0 00:02.410 --> 00:08.890 Welcome, In this lecture I'm going to show you how flexible "for" statement is. and at the end you'll learn 1 00:08.890 --> 00:11.610 about the break statements. Lets get started. 2 00:12.620 --> 00:15.020 Remember this "for" statement. 3 00:15.020 --> 00:20.840 Now I'm going to tear it apart, in a good way of course and the break statement will naturally emerge 4 00:20.870 --> 00:23.010 from its ashes. 5 00:23.050 --> 00:28.700 Let's do that! Now, remove the initialization statement 6 00:28.710 --> 00:30.200 It can also work this way. 7 00:30.900 --> 00:36.500 Assume that the i variable is declared somewhere before and outside of the "for" statement. 8 00:36.780 --> 00:38.120 Notice the separator. 9 00:38.220 --> 00:39.840 It's still there right. 10 00:39.870 --> 00:49.320 So Go can understand that there are still a condition and a post statement. ok lets continue. 11 00:49.360 --> 00:52.030 Now I also removed the post statement. 12 00:52.080 --> 00:54.560 this works too. 13 00:54.640 --> 00:57.180 offcourse I needed to increase the "i" variable myself. 14 00:57.180 --> 00:59.520 Otherwise the loop will never quit. 15 00:59.520 --> 01:05.420 I moved it after the summation. It's because I'm trying to imitate the actual "for" loop here. 16 01:05.490 --> 01:11.670 Remember Go runs the post statements after running all the statements in its block. So that's why I'm 17 01:11.700 --> 01:12.620 incrementing 18 01:12.660 --> 01:15.130 the "i" variable at the end of the loop here. 19 01:16.460 --> 01:18.550 Notice again the separators 20 01:18.710 --> 01:20.760 they are still here right. 21 01:20.780 --> 01:24.710 OK let's also remove the separators. 22 01:24.720 --> 01:29.010 This is known as the "for" statement with a single condition. 23 01:29.010 --> 01:33.270 Now let's also remove the condition. here 24 01:33.290 --> 01:40.460 this loop will never quit. It will loop forever to the infinity Its because behind the scenes the condition 25 01:40.460 --> 01:47.330 is still there! it's just hidden and it equal to true. remember for loop will continue as long as its 26 01:47.330 --> 01:48.460 condition is true. 27 01:48.520 --> 01:54.660 right? so because the hidden condition is true this loop never quit. 28 01:54.660 --> 01:58.530 By the way this loop syntax is called an infinite loop. 29 01:58.830 --> 02:04.260 As you see in the examples that infinite loops are mostly used when you don't know how many times you 30 02:04.260 --> 02:07.110 would repeat the statements. 31 02:07.170 --> 02:12.120 For example you can keep repeating the loop until the user presses the quit key. 32 02:12.150 --> 02:14.240 Or you can use it for a network server. 33 02:14.240 --> 02:17.250 which runs and listens for ever. 34 02:17.440 --> 02:19.690 So how can you end this for loop. 35 02:19.780 --> 02:22.740 Let me show you. To exit from the loop. 36 02:22.850 --> 02:29.060 I need to use a break statement. but where you type the break statement is very important because it will 37 02:29.060 --> 02:31.690 break out from the loop immediately. 38 02:31.700 --> 02:38.480 For example, if you call it here. it will quit from the loop after the summation and it will skip the following 39 02:38.510 --> 02:40.340 incr-decr statement. 40 02:40.340 --> 02:44.600 So because of this! Actually, This loop will end on its first step. 41 02:44.900 --> 02:49.360 and I don't want that. I want to add the numbers as before. right? 42 02:49.590 --> 02:52.630 So you need an If statement to properly quit from the loop. 43 02:52.820 --> 02:53.350 Let me show you 44 02:56.670 --> 02:59.170 now I'm going to type an if statement like this. 45 03:01.850 --> 03:06.150 As you can see I move the break statement inside the if statement now. 46 03:06.560 --> 03:11.800 This way the loop will only quit after the "i" variable becomes greater than 5. right? 47 03:14.490 --> 03:15.320 Let me show you this. 48 03:15.330 --> 03:16.240 In the code editor. 49 03:21.600 --> 03:30.040 first I'm going to declare the sum and "i" variable's like this since I want to add the numbers starting 50 03:30.040 --> 03:33.760 from 1 so I've initialized the "i" variable to 1 51 03:34.260 --> 03:38.890 OK then I'm going to type an infinite for statement like this. 52 03:39.230 --> 03:41.200 Just the curly braces right here. 53 03:43.120 --> 03:49.660 Inside this block, I'm going to compare whether the i variable is greater than 5 or not like this. 54 03:50.880 --> 03:58.190 So this way this program will only add numbers up to 5 and I'm going to type break statement here. 55 03:58.530 --> 04:05.990 So it will quit from the loop only when the i variable becomes greater than 5 and then I'm going to 56 04:05.990 --> 04:13.350 add the "i" variable to the sum variable like this and lastly, I'm going to increment the "i" variable 57 04:14.940 --> 04:21.440 The order of the statements is very important. as I said it's like the actual for statement now. 58 04:21.750 --> 04:29.680 So before each loop step begins go we'll check for the condition then it will add the "i" variable and the sum. 59 04:30.200 --> 04:36.990 Then at the end it will increment "i" variable and the loop will continue until the "i" variable becomes greater 60 04:36.990 --> 04:37.730 than 5. 61 04:37.920 --> 04:39.660 Then it'll break out from there. 62 04:41.260 --> 04:42.070 All right. 63 04:42.130 --> 04:43.210 Now let's run this 64 04:46.220 --> 04:50.890 as you can see it prints 15, which is just correct. 65 04:50.980 --> 04:53.030 Now let the me print the numbers here. 66 04:53.380 --> 04:56.100 So you can see what it's calculating. println i 67 04:56.120 --> 04:58.070 dash dash 68 04:58.080 --> 05:00.910 greater than arrow and sum. OK? 69 05:01.790 --> 05:03.230 OK let me run into again. 70 05:04.270 --> 05:06.160 So it adds the numbers 71 05:06.160 --> 05:08.690 step by step right. 72 05:08.830 --> 05:12.270 All right in the next lecture I'm going to show you another statement 73 05:12.460 --> 05:18.700 If these examples look simple to you don't worry at all. I prepared more challenging ones as well. 74 05:18.700 --> 05:20.460 You'll learn about them soon 75 05:20.650 --> 05:22.090 So see you in the next lecture. 76 05:22.230 --> 05:22.520 Bye Bye