1 00:00:00,630 --> 00:00:07,650 ‫Now that you know how to declare an initialize race and how to use them, it's time to use a for each 2 00:00:07,650 --> 00:00:14,370 ‫loop or to get to know the for each loop in order to get all the values of an array or put values into 3 00:00:14,370 --> 00:00:15,680 ‫an array and so forth. 4 00:00:15,690 --> 00:00:23,460 ‫So let's go ahead and create a new array, which I'm going to call nums. 5 00:00:23,460 --> 00:00:31,440 ‫So it's just going to contain some numbers and it's going to be nums equal new int let's say it has 6 00:00:31,440 --> 00:00:36,900 ‫ten values, so there should be ten values in that array and it all should be integers. 7 00:00:36,900 --> 00:00:46,560 ‫Next, I want to put values into that integer array so we get started by using the for loop here and 8 00:00:46,560 --> 00:00:54,270 ‫I'm just going to start off with zero I smaller ten and I plus plus. 9 00:00:55,710 --> 00:01:02,790 ‫So what I do here is I simply assign the value of its index to well, the value itself. 10 00:01:02,790 --> 00:01:06,360 ‫So let's go ahead and just say I. 11 00:01:06,450 --> 00:01:15,120 ‫So what that will do is at the position zero so an index zero value will be zero at index, one value 12 00:01:15,150 --> 00:01:16,440 ‫will be one. 13 00:01:17,790 --> 00:01:18,150 ‫All right. 14 00:01:18,150 --> 00:01:25,530 ‫So this is pretty much what we do with this for loop and all the way up to nine so until lower than 15 00:01:25,530 --> 00:01:25,920 ‫ten. 16 00:01:25,920 --> 00:01:27,720 ‫So up to nine. 17 00:01:28,410 --> 00:01:32,940 ‫So let's go ahead and see what that means. 18 00:01:32,940 --> 00:01:40,290 ‫So in order to run through the loop and see all the values, we can simply use a for loop again for 19 00:01:40,290 --> 00:01:52,200 ‫INT and I'm going to use j here for and j equals zero j lower ten j plus plus. 20 00:01:53,370 --> 00:02:03,840 ‫And now I just want to get the CW double tap and in here I get the element at the position zero. 21 00:02:03,840 --> 00:02:10,770 ‫So I just want to get the values out of my array and that will be equal to one. 22 00:02:11,010 --> 00:02:16,140 ‫What I need here is J and nums at the position of J. 23 00:02:16,890 --> 00:02:20,190 ‫So this is the index and that will be the actual value. 24 00:02:21,470 --> 00:02:31,850 ‫And then finally, at one point, we all need to wait for the output or for input of the user. 25 00:02:32,180 --> 00:02:32,480 ‫All right. 26 00:02:32,480 --> 00:02:33,590 ‫So let's just run that. 27 00:02:33,590 --> 00:02:38,420 ‫And then we are we get a zero element, 1 to 3 and so forth. 28 00:02:39,080 --> 00:02:41,160 ‫So that was simply what I've done here. 29 00:02:41,180 --> 00:02:47,270 ‫We could change that to let's say I don't want to enter I to it. 30 00:02:47,270 --> 00:02:50,270 ‫I want to enter I plus ten. 31 00:02:50,360 --> 00:02:51,800 ‫So let's run it again. 32 00:02:53,090 --> 00:02:55,520 ‫And at the position zero, we have the value of ten. 33 00:02:55,520 --> 00:02:58,820 ‫At the position one, we have the value of 11 and so forth. 34 00:02:59,030 --> 00:02:59,630 ‫All right. 35 00:02:59,630 --> 00:03:05,270 ‫So it doesn't have to be the same value for the index as the index itself. 36 00:03:06,170 --> 00:03:08,270 ‫So that's one way of doing it. 37 00:03:08,270 --> 00:03:11,740 ‫But what we do here is quite risky. 38 00:03:11,750 --> 00:03:19,460 ‫So, for example, if I just say up to 11 or below 11, so the means up to ten, I run into an index 39 00:03:19,460 --> 00:03:23,320 ‫out of bounds exception or actually out of range exception. 40 00:03:23,330 --> 00:03:29,420 ‫So what that means is it's trying to access an index of the array which is out of range, which is not 41 00:03:29,420 --> 00:03:36,260 ‫within the range of the array because the array only has a range of ten and we are trying to access 42 00:03:36,260 --> 00:03:39,470 ‫its 11th position and that's something that doesn't work. 43 00:03:39,470 --> 00:03:40,600 ‫So we get this error. 44 00:03:40,610 --> 00:03:44,180 ‫So in order to avoid that, we have two solutions. 45 00:03:44,180 --> 00:03:54,230 ‫One of them we use the length property, so only as long as the length is, that's how many values we 46 00:03:54,230 --> 00:03:55,190 ‫want to have. 47 00:03:55,940 --> 00:03:59,180 ‫So we run it again and now we don't get the error. 48 00:03:59,480 --> 00:04:00,770 ‫So now that's fine. 49 00:04:01,510 --> 00:04:04,900 ‫An alternative way is to use it for each loop. 50 00:04:04,990 --> 00:04:08,120 ‫So let's have a look at the structure for each loop. 51 00:04:08,140 --> 00:04:14,210 ‫First, we use the keyword for each, and then within brackets we create a new variable. 52 00:04:14,230 --> 00:04:15,850 ‫I'm just going to call it K. 53 00:04:15,850 --> 00:04:22,030 ‫And then you say in so again, a new keyword in the array name. 54 00:04:22,030 --> 00:04:23,400 ‫So in nums. 55 00:04:23,410 --> 00:04:25,980 ‫So that's the nums array name that we had here. 56 00:04:25,990 --> 00:04:33,220 ‫So we say create a variable called K and run through the whole array. 57 00:04:33,790 --> 00:04:41,170 ‫So that will do is it will simply give us the same thing actually. 58 00:04:41,170 --> 00:04:47,440 ‫But in order to run it properly, we need a counter because I want to know the position of it as well. 59 00:04:47,440 --> 00:04:53,530 ‫So I'm just going to create a counter and I'm going to use the console right line statement from here 60 00:04:53,530 --> 00:04:54,490 ‫once again. 61 00:04:54,910 --> 00:05:02,170 ‫So we have the element and now it's a J and nums, but it's the counter and K. 62 00:05:03,960 --> 00:05:09,630 ‫So K is actually the value of numbers at a specific index. 63 00:05:09,630 --> 00:05:14,730 ‫So first at index zero, then it's index one, then at index two and so forth. 64 00:05:14,730 --> 00:05:17,730 ‫So how many entries we have in nums? 65 00:05:17,730 --> 00:05:25,560 ‫It's going to loop through all of them and it's just going to in each loop say that K is the current 66 00:05:25,710 --> 00:05:27,930 ‫value of the index that it's at. 67 00:05:28,320 --> 00:05:33,000 ‫So it's first at index zero, then at index one, index two and so forth. 68 00:05:33,000 --> 00:05:34,380 ‫So if you run that again. 69 00:05:35,820 --> 00:05:42,150 ‫We see we get 10 to 19 and then again 10 to 19. 70 00:05:42,810 --> 00:05:43,170 ‫All right. 71 00:05:43,170 --> 00:05:46,350 ‫So this for each loop here, it's pretty similar to this. 72 00:05:46,350 --> 00:05:48,120 ‫For loop, it does the same thing. 73 00:05:48,690 --> 00:05:55,170 ‫So it simply runs through the array and it can be used quite effectively, which is pretty cool. 74 00:05:56,070 --> 00:05:59,970 ‫So what's the whole purpose of for each against the for loop? 75 00:06:00,000 --> 00:06:08,800 ‫So the idea here is that K has actually the same type as the values of the array itself. 76 00:06:08,820 --> 00:06:12,990 ‫In this case, we always have an integer as the loop value. 77 00:06:12,990 --> 00:06:19,020 ‫So the loop value is this j or in usual it's an I for example here in this for loop. 78 00:06:19,020 --> 00:06:25,530 ‫So it's always an integer when we loop with a for loop, but with a for each loop, the data type is 79 00:06:25,530 --> 00:06:27,630 ‫always the one of the array. 80 00:06:27,840 --> 00:06:34,020 ‫Then you don't need to specify the bounds, so you didn't need to tell when it starts and when it ends, 81 00:06:34,020 --> 00:06:38,040 ‫it always knows it just by the array itself. 82 00:06:38,040 --> 00:06:43,370 ‫So the array, let's say, has five values, then it knows, okay, it needs to run five times. 83 00:06:43,380 --> 00:06:49,170 ‫Now in terms of performance, in most cases, a for loop is faster than E for each loop. 84 00:06:49,170 --> 00:06:53,850 ‫But if you want to dig deeper into it, you should simply check it out yourself. 85 00:06:53,850 --> 00:06:59,880 ‫So create complex for each and for loops and test them against each other to see what performs better. 86 00:07:01,870 --> 00:07:02,260 ‫All right. 87 00:07:02,260 --> 00:07:04,130 ‫Now, a little challenge for you. 88 00:07:04,150 --> 00:07:05,860 ‫I'm just going to type it out here. 89 00:07:07,480 --> 00:07:07,840 ‫All right. 90 00:07:07,840 --> 00:07:09,110 ‫So that one's pretty simple. 91 00:07:09,130 --> 00:07:11,920 ‫Create an array with five of your best friends. 92 00:07:12,640 --> 00:07:18,010 ‫The data type should be clear and then create a for each loop which greets all of them. 93 00:07:20,240 --> 00:07:22,310 ‫So post the video and try to do that. 94 00:07:23,210 --> 00:07:23,720 ‫All right. 95 00:07:23,720 --> 00:07:24,890 ‫I hope you tried it. 96 00:07:24,890 --> 00:07:28,730 ‫So I'm just going to get rid of all this code here. 97 00:07:30,360 --> 00:07:33,200 ‫And I'm going to create an array. 98 00:07:33,210 --> 00:07:39,840 ‫String, my friends, is equal to Michael. 99 00:07:43,950 --> 00:07:52,140 ‫Lutz, Elia, Andy and Daniel. 100 00:07:53,810 --> 00:07:54,120 ‫Right. 101 00:07:54,120 --> 00:07:55,320 ‫So these are my friends. 102 00:07:55,320 --> 00:08:00,570 ‫And now I created for each loop which runs through all of them. 103 00:08:00,570 --> 00:08:05,040 ‫So string name in my friends. 104 00:08:07,820 --> 00:08:09,500 ‫And now let's greet all of them. 105 00:08:09,530 --> 00:08:12,560 ‫So, CW double tap console, right? 106 00:08:13,160 --> 00:08:14,570 ‫Hi there. 107 00:08:14,660 --> 00:08:18,950 ‫And I'm going to use zero. 108 00:08:19,910 --> 00:08:21,380 ‫Come on, my friend. 109 00:08:23,630 --> 00:08:26,120 ‫And this one going to be the name. 110 00:08:27,200 --> 00:08:37,310 ‫And finally at the end console, the right key so that we can see or can finish the program or solve. 111 00:08:37,970 --> 00:08:39,110 ‫So let's run that. 112 00:08:40,460 --> 00:08:41,390 ‫So there we are. 113 00:08:41,420 --> 00:08:42,680 ‫Hi there, Michael, my friend. 114 00:08:42,710 --> 00:08:43,940 ‫Hi there, my friend. 115 00:08:43,940 --> 00:08:45,950 ‫Either my friend and so forth. 116 00:08:45,950 --> 00:08:51,110 ‫So now I read it to all of my friends and I didn't need to do that manually. 117 00:08:51,110 --> 00:08:52,220 ‫One by one. 118 00:08:52,220 --> 00:08:56,810 ‫I simply wrote a for each loop for that and I greeted all of them. 119 00:08:56,810 --> 00:08:59,450 ‫So that's already a very simple bot. 120 00:08:59,450 --> 00:09:04,820 ‫What we simply agreed our friends, and it could be more complex so we could, let's say, create a 121 00:09:04,820 --> 00:09:12,260 ‫list of emails here, and then we could create a email service or mailing service which will then create 122 00:09:12,260 --> 00:09:15,170 ‫an email and send it out to all of them one by one. 123 00:09:15,320 --> 00:09:18,440 ‫And we'll do it with a for each loop automatically. 124 00:09:18,890 --> 00:09:19,460 ‫All right. 125 00:09:19,460 --> 00:09:24,260 ‫So you've seen how to use race, but there is a lot more to know about the race. 126 00:09:24,260 --> 00:09:27,530 ‫And we're going to have a look at that in the next video. 127 00:09:27,530 --> 00:09:28,520 ‫So see you there.