1 00:00:04,740 --> 00:00:06,900 in the previous video we looked at 2 00:00:06,900 --> 00:00:09,389 looping to repeat a block of code using 3 00:00:09,389 --> 00:00:11,460 the while loop. In this video we're going 4 00:00:11,460 --> 00:00:13,470 to review another kind of loop called a 5 00:00:13,470 --> 00:00:15,630 for loop. Now a for loop does a similar 6 00:00:15,630 --> 00:00:18,000 job to a while loop but it's generally 7 00:00:18,000 --> 00:00:19,680 used when you have some sort of limit 8 00:00:19,680 --> 00:00:21,570 that determines how many times you need 9 00:00:21,570 --> 00:00:24,689 to repeat a block of code so we're a 10 00:00:24,689 --> 00:00:26,700 while loop we'll keep looping as long as 11 00:00:26,700 --> 00:00:29,009 the condition remains true a for loop 12 00:00:29,009 --> 00:00:31,140 will execute a set number of times now 13 00:00:31,140 --> 00:00:33,059 we've already looked at the for loop 14 00:00:33,059 --> 00:00:34,950 when we created a show inventory 15 00:00:34,950 --> 00:00:37,530 function in our player class it's a very 16 00:00:37,530 --> 00:00:39,539 basic loop that just iterates over each 17 00:00:39,539 --> 00:00:41,640 item in the array list and prints the 18 00:00:41,640 --> 00:00:44,160 items details so let's actually go ahead 19 00:00:44,160 --> 00:00:47,370 and make it do a bit more now s our 20 00:00:47,370 --> 00:00:48,870 players wander around killing monsters 21 00:00:48,870 --> 00:00:51,269 they'll pick up loot throughout the game 22 00:00:51,269 --> 00:00:53,579 so let's get the loop to calculate a 23 00:00:53,579 --> 00:00:56,249 score by adding up the value of all 24 00:00:56,249 --> 00:00:58,170 their loot at the end of the previous 25 00:00:58,170 --> 00:01:00,030 video they just chase the Dracula away 26 00:01:00,030 --> 00:01:01,739 from his hoard which means there should 27 00:01:01,739 --> 00:01:03,179 be a fair bit of loot for them to 28 00:01:03,179 --> 00:01:05,610 collect what I'm gonna do is comment out 29 00:01:05,610 --> 00:01:10,970 the world loop in our main function 30 00:01:10,970 --> 00:01:13,110 to keep the app with a bit clearer and 31 00:01:13,110 --> 00:01:14,220 then we'll get the player to pick up 32 00:01:14,220 --> 00:01:16,470 Dracula's hoard well so what we'll do is 33 00:01:16,470 --> 00:01:18,119 we'll also comment out all this code up 34 00:01:18,119 --> 00:01:20,280 here the troll code as well so comment 35 00:01:20,280 --> 00:01:23,310 all that out and on the wallet that we 36 00:01:23,310 --> 00:01:26,340 added in the last video let's go ahead 37 00:01:26,340 --> 00:01:31,280 and add a new player so Val Kernan 38 00:01:31,280 --> 00:01:35,280 equals player parentheses after the 39 00:01:35,280 --> 00:01:41,130 double and double quotes Conan Conan dot 40 00:01:41,130 --> 00:01:46,640 get loot parentheses loot parentheses 41 00:01:46,640 --> 00:01:53,240 invisibility comma loop type dot armor 42 00:01:53,240 --> 00:01:57,750 comma 4.0 let's copy at this line I'll 43 00:01:57,750 --> 00:01:59,820 make a few more changes there's another 44 00:01:59,820 --> 00:02:03,810 nine items but obviously change them as 45 00:02:03,810 --> 00:02:05,070 we go through so we'll do invisibility 46 00:02:05,070 --> 00:02:10,369 for the first one mithril for the second one 47 00:02:10,369 --> 00:02:17,299 third one being of speed, north one red 48 00:02:17,299 --> 00:02:28,629 potion cursed shield brass ring 49 00:02:28,629 --> 00:02:31,250 well mostly missing game type mixing 50 00:02:31,250 --> 00:02:34,220 game types here some of this stuff is 51 00:02:34,220 --> 00:02:35,480 from different games but that's a run at 52 00:02:35,480 --> 00:02:37,549 all served well as an example chain 53 00:02:37,549 --> 00:02:45,190 maille gold ring health potion and 54 00:02:45,190 --> 00:02:49,519 silver ring is the last one for what we 55 00:02:49,519 --> 00:02:52,459 need to change the item top so which was 56 00:02:52,459 --> 00:02:53,810 also am i sorry you can leave that one 57 00:02:53,810 --> 00:02:55,849 third one ring of spain is obviously a 58 00:02:55,849 --> 00:03:00,790 ring red potion obviously is a potion 59 00:03:00,790 --> 00:03:03,680 curse shield well that's obviously armor 60 00:03:03,680 --> 00:03:07,569 got a brass ring obviously a ring 61 00:03:07,569 --> 00:03:11,319 chainmail armor gold ring is a ring 62 00:03:11,319 --> 00:03:16,160 health potion potion and silver ring is 63 00:03:16,160 --> 00:03:18,739 obviously a ring the last thing I need 64 00:03:18,739 --> 00:03:20,900 to do now is give the values to them so 65 00:03:20,900 --> 00:03:22,549 invisibility that should have been a 66 00:03:22,549 --> 00:03:24,459 potion as wealth has changed that 67 00:03:24,459 --> 00:03:28,010 obviously the visibility's not armor 68 00:03:28,010 --> 00:03:31,819 okay so 4.04 the first one we're going 69 00:03:31,819 --> 00:03:33,290 to permit your alarm we're going to give 70 00:03:33,290 --> 00:03:36,859 that 183 it's the score ring of speed 71 00:03:36,859 --> 00:03:41,690 let's make that 25 okay red potion will 72 00:03:41,690 --> 00:03:46,340 go with to curse shield that's a bad 73 00:03:46,340 --> 00:03:47,870 thing so we're going to go a negative 74 00:03:47,870 --> 00:03:52,699 eight brass ring value of 1 chainmail 75 00:03:52,699 --> 00:03:54,410 let's go with a bail of 4 for that solid 76 00:03:54,410 --> 00:03:57,109 then on the default gold ring let's 77 00:03:57,109 --> 00:04:00,620 change the fourth over 12 health potion 78 00:04:00,620 --> 00:04:04,459 let's go with 3 for that then the silver ring 79 00:04:04,459 --> 00:04:08,930 lastly we'll go with a value of 6 okay 80 00:04:08,930 --> 00:04:12,739 and then lastly as to a conan dots show 81 00:04:12,739 --> 00:04:19,089 inventory okay let's run that 82 00:04:19,089 --> 00:04:20,709 you can see there that Conan is they've 83 00:04:20,709 --> 00:04:23,260 got quite a stash of loot but what's all 84 00:04:23,260 --> 00:04:24,130 the loot worth 85 00:04:24,130 --> 00:04:26,800 so let's modify the for loop to loop 86 00:04:26,800 --> 00:04:28,750 through all the items in Conan's stash 87 00:04:28,750 --> 00:04:31,960 and work out the total value so we're 88 00:04:31,960 --> 00:04:33,520 gonna go back to the show inventory 89 00:04:33,520 --> 00:04:39,970 function in the player class ok and what 90 00:04:39,970 --> 00:04:41,260 I'm gonna do start out by making a 91 00:04:41,260 --> 00:04:43,480 mistake here because it's such a common 92 00:04:43,480 --> 00:04:45,430 mistake to make and it's a minor thing 93 00:04:45,430 --> 00:04:47,320 so as long as you can spawn it quickly 94 00:04:47,320 --> 00:04:49,180 you can get back to writing code without 95 00:04:49,180 --> 00:04:51,430 losing your train of thought so what 96 00:04:51,430 --> 00:04:53,890 we're going to do after the printing of 97 00:04:53,890 --> 00:04:56,560 the items will actually print learn of 98 00:04:56,560 --> 00:04:59,590 the item we're going to put total plus 99 00:04:59,590 --> 00:05:03,700 equals item value but we also need to 100 00:05:03,700 --> 00:05:06,040 add that variable so we add that at the 101 00:05:06,040 --> 00:05:07,660 first line of the supplementary function 102 00:05:07,660 --> 00:05:11,080 they're total equals zero so we come 103 00:05:11,080 --> 00:05:19,990 down here and then a cup of lines total 104 00:05:19,990 --> 00:05:25,390 score is : dollar total so you can see 105 00:05:25,390 --> 00:05:28,240 that on the first line line 47 I started 106 00:05:28,240 --> 00:05:30,640 out by creating gather variable total 107 00:05:30,640 --> 00:05:32,800 and it's set it initially to zero will 108 00:05:32,800 --> 00:05:34,690 be accumulating the total value of the 109 00:05:34,690 --> 00:05:37,150 loot in this variable but the mistake I 110 00:05:37,150 --> 00:05:40,030 made was to initialize it to zero this 111 00:05:40,030 --> 00:05:42,850 is the code here on line 47 by doing 112 00:05:42,850 --> 00:05:45,610 that I'll make it an int so we get an error 113 00:05:45,610 --> 00:05:47,560 we try to add a double value to it down 114 00:05:47,560 --> 00:05:48,970 here you can see their type mismatch 115 00:05:48,970 --> 00:05:52,570 quiet int found double even though the 116 00:05:52,570 --> 00:05:54,370 error is showing here the actual problem 117 00:05:54,370 --> 00:05:56,770 was caused by us initializing it to zero 118 00:05:56,770 --> 00:06:00,130 chance of to 0.0 you can see the error 119 00:06:00,130 --> 00:06:03,190 and then disappears so that's a very 120 00:06:03,190 --> 00:06:05,080 common mistake to make as I said so 121 00:06:05,080 --> 00:06:07,180 watch out for that and that a very easy 122 00:06:07,180 --> 00:06:08,620 solution is you can see up there just 123 00:06:08,620 --> 00:06:10,030 making sure that you're initializing it 124 00:06:10,030 --> 00:06:12,850 to the correct type all right so each 125 00:06:12,850 --> 00:06:14,590 time around the loop we had the items 126 00:06:14,590 --> 00:06:17,200 value to total that way we build up the 127 00:06:17,200 --> 00:06:20,050 total value of our inventory and when 128 00:06:20,050 --> 00:06:21,460 the loop terminates been printing out 129 00:06:21,460 --> 00:06:24,160 the total value of all the loot so we 130 00:06:24,160 --> 00:06:30,190 actually run this now 131 00:06:30,190 --> 00:06:32,780 you can see we got the total score the 132 00:06:32,780 --> 00:06:35,750 total loot value if you will of 232 133 00:06:35,750 --> 00:06:38,000 that's the total value of all the loot 134 00:06:38,000 --> 00:06:40,730 now being of a suspicious disposition 135 00:06:40,730 --> 00:06:43,070 Conan examined that cursed shield 136 00:06:43,070 --> 00:06:45,590 carefully and decided to leave it in the 137 00:06:45,590 --> 00:06:47,510 Dracula's lair so I'm going to comment 138 00:06:47,510 --> 00:06:52,730 that line out and I've actually run this 139 00:06:52,730 --> 00:06:55,460 again we should fire that there's less 140 00:06:55,460 --> 00:06:57,260 items now in the inventory fewer items 141 00:06:57,260 --> 00:06:59,090 and the loop goes around 9 times instead 142 00:06:59,090 --> 00:07:01,370 of 10 and the score has now gone up to 143 00:07:01,370 --> 00:07:04,730 240 of course because we've removed the 144 00:07:04,730 --> 00:07:07,850 curse shield which was adding negative 8 145 00:07:07,850 --> 00:07:10,580 to our total so that's a for loop in 146 00:07:10,580 --> 00:07:12,800 Kotlin other languages such as c plus 147 00:07:12,800 --> 00:07:14,870 possible jar i have another form of for 148 00:07:14,870 --> 00:07:17,240 loop in fact this form is called the for 149 00:07:17,240 --> 00:07:19,700 each in those languages but Kotlin 150 00:07:19,700 --> 00:07:21,830 doesn't have another form in Kotlin of 151 00:07:21,830 --> 00:07:24,050 for loop works by iterating over a set 152 00:07:24,050 --> 00:07:26,150 of values if you don't have a set of 153 00:07:26,150 --> 00:07:28,040 values to iterate over i just want to 154 00:07:28,040 --> 00:07:29,480 repeat a block of code a certain number 155 00:07:29,480 --> 00:07:31,580 of times what you can do is create a 156 00:07:31,580 --> 00:07:34,490 range of values so to see what I mean 157 00:07:34,490 --> 00:07:36,920 let's comment out all this conan code 158 00:07:36,920 --> 00:07:41,590 you know main method our main function 159 00:07:41,590 --> 00:07:45,200 let's put back the Dracula slaying code 160 00:07:45,200 --> 00:07:47,330 so just as code here that starts with 161 00:07:47,330 --> 00:07:50,390 the vampire king dracula will uncomment 162 00:07:50,390 --> 00:07:55,220 that we're also going to change it in 163 00:07:55,220 --> 00:07:58,700 line 23 that was take was inflicting 12 164 00:07:58,700 --> 00:08:00,560 points of damage has changed that number 165 00:08:00,560 --> 00:08:05,720 to 80 then we actually run this she'll 166 00:08:05,720 --> 00:08:06,650 be able to see the damage being 167 00:08:06,650 --> 00:08:09,080 inflicted and directly eventually runs 168 00:08:09,080 --> 00:08:11,150 away so you can see now there's a random 169 00:08:11,150 --> 00:08:13,190 element to it when Dracula dodges so you 170 00:08:13,190 --> 00:08:14,660 may want to simulate the battle a few 171 00:08:14,660 --> 00:08:16,550 times you can see in this particular 172 00:08:16,550 --> 00:08:18,620 case he didn't dodge at all so we try 173 00:08:18,620 --> 00:08:24,530 running it again this time he did manage 174 00:08:24,530 --> 00:08:30,250 to do some dodging 175 00:08:30,250 --> 00:08:34,340 okay eventually runs away now we could 176 00:08:34,340 --> 00:08:36,650 run it repeatedly but just to show what 177 00:08:36,650 --> 00:08:39,049 I mean about creating a range I'm gonna 178 00:08:39,049 --> 00:08:41,659 get the program to run all this code ten 179 00:08:41,659 --> 00:08:44,000 times only so what we need to do to do 180 00:08:44,000 --> 00:08:45,770 that is we want to wrap all this code in 181 00:08:45,770 --> 00:08:48,470 a for loop so I'm going to go ahead and 182 00:08:48,470 --> 00:08:52,390 create the for loop so for parentheses I 183 00:08:52,390 --> 00:08:57,290 in one space two dots two periods in 184 00:08:57,290 --> 00:08:59,390 other words ten and if you create a code 185 00:08:59,390 --> 00:09:01,430 block I'm going to put all that code in 186 00:09:01,430 --> 00:09:06,230 that code block okay cut that paste it 187 00:09:06,230 --> 00:09:08,420 into that code block we created for the 188 00:09:08,420 --> 00:09:10,190 for loop and what I'm going to do is 189 00:09:10,190 --> 00:09:12,350 just add a row of dashes just to make it 190 00:09:12,350 --> 00:09:14,750 easier to see when each batter wins so 191 00:09:14,750 --> 00:09:16,820 I'm going to put that in here before the 192 00:09:16,820 --> 00:09:21,140 closing for loop printing this would add 193 00:09:21,140 --> 00:09:25,520 some dashes here so now when I run the 194 00:09:25,520 --> 00:09:30,920 program we get ten runs of the battle 195 00:09:30,920 --> 00:09:33,320 with Dracula should be dodging at 196 00:09:33,320 --> 00:09:35,930 different times in each one you can see 197 00:09:35,930 --> 00:09:37,880 each batter was separated by the dashes 198 00:09:37,880 --> 00:09:42,250 that we added 199 00:09:42,250 --> 00:09:45,610 and so on run up to the top so we had to 200 00:09:45,610 --> 00:09:47,530 provide a range of values for the for 201 00:09:47,530 --> 00:09:49,390 loop to iterate over we did that on line 202 00:09:49,390 --> 00:09:52,090 13 because there's no list to loop 203 00:09:52,090 --> 00:09:53,920 through. Alright so that's the for loop 204 00:09:53,920 --> 00:09:55,360 so I'm going to stop the video here and 205 00:09:55,360 --> 00:09:57,400 in the next one we'll look at the third 206 00:09:57,400 --> 00:09:59,410 top of loop, we'll be using the do-while 207 00:09:59,410 --> 00:10:03,690 loop, so I'll see you in the next video.