1 00:00:00,600 --> 00:00:03,620 Hello and welcome to this lecture. 2 00:00:03,720 --> 00:00:15,270 For each groups for each loops are useful they are used to execute a block of code on each element in 3 00:00:15,270 --> 00:00:21,260 an array or a collection of items. 4 00:00:22,270 --> 00:00:34,110 The for each loops are quite useful for displaying each items in an array or a collection of items. 5 00:00:34,380 --> 00:00:43,160 So the syntax or basically the syntax refers to how you specify or define a for each loop. 6 00:00:43,440 --> 00:00:50,910 So you start with a word for each and then you specify the variable type in the array or collection 7 00:00:51,660 --> 00:00:57,270 followed by the code that will be executed. 8 00:00:57,540 --> 00:01:03,270 So let's have a look at how we illustrate the for each loop 9 00:01:06,000 --> 00:01:07,360 to save time. 10 00:01:07,380 --> 00:01:12,920 I've already written out some code here which is this piece of code here. 11 00:01:13,550 --> 00:01:19,200 Okay so I'll just show you what I have done. 12 00:01:20,650 --> 00:01:31,560 I previously used some code here for to illustrate a loop basically a while loop a do while loop. 13 00:01:31,570 --> 00:01:36,940 So I've commented out the that piece of code here you can see it in green here. 14 00:01:36,940 --> 00:01:44,870 So the code we are focusing on here is a block of code here to begin. 15 00:01:44,920 --> 00:01:52,200 I created a variable type and the variable type the type is a string. 16 00:01:52,210 --> 00:01:59,700 Notice of put square brackets in there the square brackets indicates that it is an array. 17 00:01:59,710 --> 00:02:07,150 That means several an array basically consists of several variables stored as one. 18 00:02:07,150 --> 00:02:11,370 So I've given the name of the array of variable. 19 00:02:11,380 --> 00:02:15,430 I've given it the name of d All right. 20 00:02:15,430 --> 00:02:20,240 So that's what I've called it and I have initialized it by initializing it. 21 00:02:20,260 --> 00:02:22,230 I have added values to it. 22 00:02:22,240 --> 00:02:31,000 Notice I've used the curly braces the left and the right and I have specified all these are all elements 23 00:02:31,150 --> 00:02:32,550 of this array. 24 00:02:32,560 --> 00:02:43,680 Here you can always tell or identify an array with square brackets anyways square brackets. 25 00:02:43,720 --> 00:02:54,060 You know there's some kind of I re going on they are available so the array is called days and even 26 00:02:54,220 --> 00:02:58,040 initialize it an assigned value using the equals to sign. 27 00:02:58,360 --> 00:03:07,270 And these are the various objects or elements that will consist of this array because the string the 28 00:03:07,300 --> 00:03:10,060 data type is a string. 29 00:03:10,060 --> 00:03:19,110 Hence the text values are enclosed in quotes and each of the values are separated by comma. 30 00:03:19,120 --> 00:03:20,760 Here on Line 23. 31 00:03:20,770 --> 00:03:24,390 This is where r for each begins. 32 00:03:24,390 --> 00:03:35,710 So with defining the for each loop this variable type the string is what will be used to collect the 33 00:03:35,710 --> 00:03:38,770 items from the collection or the array. 34 00:03:39,070 --> 00:03:47,540 Okay so string so the for each here and so have the for each and inside it for each parentheses are 35 00:03:47,560 --> 00:04:02,050 specified a variable type or a data type which is string and of all so also declared this here day for 36 00:04:02,050 --> 00:04:06,340 the for each sub declared is string chord. 37 00:04:06,450 --> 00:04:07,090 D. 38 00:04:08,110 --> 00:04:11,990 Okay in for the for each so declare this string. 39 00:04:12,040 --> 00:04:20,900 This value code day in days so the in days is referring to this array here. 40 00:04:21,030 --> 00:04:21,640 All right. 41 00:04:23,740 --> 00:04:27,000 So what this is do in this string here. 42 00:04:27,280 --> 00:04:35,390 It will use this day to pull the value from this day's array. 43 00:04:35,440 --> 00:04:43,270 So this is what is going to be used to pick out all these values from this day's array. 44 00:04:43,300 --> 00:04:52,480 So when I click on the button it will first of for display the current day and then it will go on to 45 00:04:52,480 --> 00:04:59,770 list each of the rest of the days like Monday Tuesday Wednesday until it gets to Saturday and it will 46 00:04:59,830 --> 00:05:00,880 then exit 47 00:05:03,800 --> 00:05:12,190 and it will also display this block of code here to display the day is a start with Sunday Monday Tuesday 48 00:05:12,220 --> 00:05:13,480 and so on. 49 00:05:13,480 --> 00:05:17,350 OK so let's give that a test and see how it works. 50 00:05:17,350 --> 00:05:20,020 Click Start. 51 00:05:20,020 --> 00:05:20,400 All right. 52 00:05:20,410 --> 00:05:27,270 So if I click on this button it will go through and show all the elements in that array. 53 00:05:27,400 --> 00:05:31,350 So we just issued start with a Sunday and with a Saturday. 54 00:05:31,360 --> 00:05:33,340 So I click on the button. 55 00:05:33,340 --> 00:05:35,910 The day is a Sunday click again. 56 00:05:36,010 --> 00:05:45,590 Monday Tuesday Wednesday Thursday Friday Saturday and then it exits. 57 00:05:45,720 --> 00:05:49,150 Okay so that's basically what the. 58 00:05:49,180 --> 00:05:59,440 For each does so it will loop through the elements and display them the elements in an array or a collection 59 00:06:01,150 --> 00:06:09,040 so the values from the arrays are retreat and displayed one after the other. 60 00:06:09,730 --> 00:06:12,100 Thank you so much for watching this video. 61 00:06:12,100 --> 00:06:13,990 Take care and buy for them.