1 00:00:00,260 --> 00:00:02,140 You've already been introduced to the four loop. 2 00:00:02,140 --> 00:00:03,580 Where we incremental number up or 3 00:00:03,580 --> 00:00:07,550 down until we reach whatever condition we decided on at the outset. 4 00:00:07,550 --> 00:00:10,430 This is great for adding up the numbers from one to fifty. 5 00:00:10,430 --> 00:00:12,990 But what if we wanted to print out a personalized letter to 6 00:00:12,990 --> 00:00:13,890 every name on a list. 7 00:00:14,890 --> 00:00:18,380 Well, you could count the number of items in the list and use a for loop 8 00:00:18,380 --> 00:00:22,330 to go through each item beginning with the first and ending with the twentieth. 9 00:00:22,330 --> 00:00:26,000 But it turns out there's an easier way, the four in loop. 10 00:00:26,000 --> 00:00:30,350 For this example I just gave, a four in loop would simply say for every name on 11 00:00:30,350 --> 00:00:34,960 the list print out a personalized letter with that name when you run out of names. 12 00:00:34,960 --> 00:00:36,760 No need to iterate through the loop anymore. 13 00:00:38,270 --> 00:00:42,830 Let's jump into our box office plus project, and see how we can put it to use. 14 00:00:42,830 --> 00:00:44,970 Now we haven't visited this project in a while, so 15 00:00:44,970 --> 00:00:47,240 let's do a quick review of what it does. 16 00:00:47,240 --> 00:00:51,360 If you don't have it handy anymore, check below to download the starter code. 17 00:00:51,360 --> 00:00:54,850 This project was built to help the box office at a movie theater. 18 00:00:54,850 --> 00:00:58,420 This main function has been changed to help us charge our movie customers 19 00:00:58,420 --> 00:01:02,400 the correct price based on a few factors like the customer's age, 20 00:01:03,860 --> 00:01:08,770 whether it's a matinee and whether the customer also happens to be an employee. 21 00:01:08,770 --> 00:01:12,830 As you can imagine, the value of those boules in combination with the age, 22 00:01:12,830 --> 00:01:15,270 can result in different ticket prices. 23 00:01:15,270 --> 00:01:18,490 Well it turns out that our movie theater loves our little program. 24 00:01:18,490 --> 00:01:22,000 They love it so much, in fact, they'd like us to add a couple new features. 25 00:01:22,000 --> 00:01:26,510 Those features are the ability to enter multiple ages at the same time, 26 00:01:26,510 --> 00:01:27,730 charging the correct price for 27 00:01:27,730 --> 00:01:32,580 each, and the ability to add sales tax to the complete order. 28 00:01:32,580 --> 00:01:37,280 Well, we already have all this wonderful code and logic down here. 29 00:01:37,280 --> 00:01:38,870 It'd be a shame to waste it. 30 00:01:38,870 --> 00:01:41,800 It determines the price of the tickets based on our boules. 31 00:01:41,800 --> 00:01:46,130 So, it seems like we should be able to put this code inside of a loop, 32 00:01:46,130 --> 00:01:49,390 feed it a bunch of ages, and then get a total. 33 00:01:49,390 --> 00:01:52,280 For this will use the four in loop. 34 00:01:52,280 --> 00:01:54,800 Now before I go giving away all the answers. 35 00:01:54,800 --> 00:01:56,860 Take a minute pause the video and 36 00:01:56,860 --> 00:02:00,420 make a note of what variables we'll need to accomplish this task. 37 00:02:00,420 --> 00:02:03,040 And any steps that you think we'll need to add to the logic. 38 00:02:05,680 --> 00:02:08,240 Okay, let's start with the variables. 39 00:02:08,240 --> 00:02:13,067 First, will need a subtotal for the cost of the tickets, 40 00:02:13,067 --> 00:02:16,800 float, subtotal, and we'll set it to 0. 41 00:02:16,800 --> 00:02:25,010 Next, will want to float told the tax rate, which will set at 5%. 42 00:02:25,010 --> 00:02:29,330 After that, we'll need a float to hold the grand total after we add the tax. 43 00:02:31,950 --> 00:02:33,820 We can set that equal to zero, as well. 44 00:02:35,660 --> 00:02:40,530 Finally, we'll need a variable to hold all of the ages we want to input. 45 00:02:40,530 --> 00:02:43,290 Now, for this one, we'll want to use a collection type. 46 00:02:43,290 --> 00:02:44,800 Specifically, an NS array. 47 00:02:45,910 --> 00:02:48,806 We can declare that like NSArray. 48 00:02:51,026 --> 00:02:54,246 AgesArray, and then we'll use the literal syntax. 49 00:03:03,266 --> 00:03:07,381 So that would be an array of a 5 year old, another 5 year old, 50 00:03:07,381 --> 00:03:11,310 a 14 year old, 42 year old, and a 77 year old. 51 00:03:11,310 --> 00:03:15,030 In real life that isn't too far fetched it could be a grandmother taking her son and 52 00:03:15,030 --> 00:03:18,260 her three grandchildren to a movie from the perspective of the box 53 00:03:18,260 --> 00:03:22,710 office attendant It would certainly be nice just to type in all the ages at once 54 00:03:22,710 --> 00:03:24,140 hit a button and get a total. 55 00:03:25,210 --> 00:03:26,980 So how would we build our for in loop? 56 00:03:28,030 --> 00:03:29,240 Let's start with the shell, and 57 00:03:29,240 --> 00:03:32,310 then we'll decide what portion of our existing code should go inside. 58 00:03:32,310 --> 00:03:37,525 We'll start right up here, and we'll begin with the key word for. 59 00:03:40,365 --> 00:03:43,496 Inside the parentheses, we're going to want to say for 60 00:03:43,496 --> 00:03:45,300 each item x in the collection y. 61 00:03:46,400 --> 00:03:51,990 Since the items in our NSarray of ages are NSNumbers, we would say for any NSNumber 62 00:03:54,280 --> 00:03:58,180 and will call that age in agesArray. 63 00:03:59,880 --> 00:04:04,850 And then we can delete this stuff here and this right here, 64 00:04:06,550 --> 00:04:10,760 so this means for every NSNumber in the agesArray, 65 00:04:10,760 --> 00:04:17,140 create an NSNumber variable called age and send it once through our for loop. 66 00:04:17,140 --> 00:04:22,030 After that single iteration is complete, create a new variable called age, 67 00:04:22,030 --> 00:04:25,050 assign it the next value in the array and send it through the loop. 68 00:04:26,060 --> 00:04:30,360 Next we have our curly braces where we'll put the meat of our loop. 69 00:04:30,360 --> 00:04:33,240 Now instead of cutting and pasting all this logic here, 70 00:04:33,240 --> 00:04:37,330 I'm just going to delete the last curly brace, and I'll put it down here. 71 00:04:38,880 --> 00:04:43,540 After all of the logic that decides what price to charge the customer. 72 00:04:44,820 --> 00:04:48,940 Now we're almost there, though we'll need a few more parts. 73 00:04:48,940 --> 00:04:54,260 First, up top, we need to convert the NS numbers in our array into an int, 74 00:04:55,540 --> 00:04:58,290 and then we'll set the customer price variable to that. 75 00:04:59,820 --> 00:05:03,450 When we talked about variables being boxes and those boxes are sized and 76 00:05:03,450 --> 00:05:06,110 shaped specifically for different data types. 77 00:05:06,110 --> 00:05:09,000 Well here we're playing a little trick where we translate the contents of 78 00:05:09,000 --> 00:05:09,770 one box. 79 00:05:09,770 --> 00:05:12,340 So it fits nicely into another and 80 00:05:12,340 --> 00:05:16,930 we would do that simply with customer price equals. 81 00:05:16,930 --> 00:05:25,577 I'm sorry. Customer age equals age int value. 82 00:05:28,417 --> 00:05:31,907 The last thing we'll need to add is a line after all of the logic, 83 00:05:31,907 --> 00:05:35,220 where we add the individual customer price to the subtotal. 84 00:05:36,340 --> 00:05:40,260 We'll want to do that still inside of our for loop, and 85 00:05:40,260 --> 00:05:44,010 that line would simply be subtotal equals 86 00:05:45,410 --> 00:05:50,070 the previous subtotal plus the customerPrice. 87 00:05:50,070 --> 00:05:53,390 Just to get a little more clarity, we could also add a log statement. 88 00:05:53,390 --> 00:05:54,752 Let's do that down here, and 89 00:05:54,752 --> 00:05:57,707 that'll show us the math step-by-step with each iteration. 90 00:06:16,920 --> 00:06:22,641 By the way, this token right here, that's just how we insert a float into our log. 91 00:06:22,641 --> 00:06:28,041 Okay, let's drop in a breakpoint down here and see how this goes. 92 00:06:31,841 --> 00:06:35,467 All right, we see that we've gone through our four end loop five times for 93 00:06:35,467 --> 00:06:38,550 the five different ages but something's funny. 94 00:06:38,550 --> 00:06:39,500 Can you spot what it is? 95 00:06:41,970 --> 00:06:45,210 It looks like our customers, the 42 year old, and 96 00:06:45,210 --> 00:06:49,820 the 14 year old are being charged 6.50 just like the f5 year old and 97 00:06:49,820 --> 00:06:53,930 the 77 year old, even though they don't get an age discount. 98 00:06:53,930 --> 00:06:55,981 Pause the video and try to figure out what's happening. 99 00:06:58,321 --> 00:06:59,481 Did you get it? 100 00:06:59,481 --> 00:07:04,083 Well what's happening is actually an unwanted effect of putting our logic in 101 00:07:04,083 --> 00:07:04,590 a loop. 102 00:07:05,810 --> 00:07:08,980 You see if we look up here. 103 00:07:11,460 --> 00:07:14,340 When we run our first age five through the loop. 104 00:07:14,340 --> 00:07:20,280 We satisfy this condition up here, and we set our age discount to yes. 105 00:07:20,280 --> 00:07:25,570 When it goes through again, the second time for 5, again, we set it for yes. 106 00:07:25,570 --> 00:07:29,220 However, on our third iteration, when the age of fourteen, 107 00:07:29,220 --> 00:07:34,840 we don't satisfy this condition, but the ageDiscount has already been set to yes, 108 00:07:34,840 --> 00:07:38,480 because it exists outside of the loop. 109 00:07:39,560 --> 00:07:40,710 There's an easy fix for that. 110 00:07:42,290 --> 00:07:48,470 We can simply put in else here to set age discount 111 00:07:48,470 --> 00:07:54,910 equal to no, whenever the above condition is not met. 112 00:07:54,910 --> 00:07:56,841 Let's run our code again and see what happens. 113 00:08:00,301 --> 00:08:01,801 Okay, much better. 114 00:08:01,801 --> 00:08:05,041 Our younguns and our elder get the discount price, 115 00:08:05,041 --> 00:08:09,120 and the 14 year old and 42 year old have to pay 8.50 each. 116 00:08:10,780 --> 00:08:13,350 Okay, just a little more to go. 117 00:08:13,350 --> 00:08:15,520 Now what we need to do is add the sales tax. 118 00:08:16,660 --> 00:08:20,400 Now since we just want to add the sales tax to the subtotal once, 119 00:08:20,400 --> 00:08:22,770 rather than to each individual ticket, 120 00:08:22,770 --> 00:08:27,460 we'll want to put that inside our main function, but outside of the loop. 121 00:08:27,460 --> 00:08:29,940 And that would be down here. 122 00:08:29,940 --> 00:08:35,100 We can do that pretty simply with a line that says, the grand total should equal 123 00:08:35,100 --> 00:08:42,950 the subtotal+(subtotal * taxRate). 124 00:08:42,950 --> 00:08:47,190 And incase you're curious, no we don't actually need the parentheses here. 125 00:08:47,190 --> 00:08:51,280 The order of operations would make sure that this happens first. 126 00:08:51,280 --> 00:08:55,390 However, I personally like to add extra parentheses sometimes, 127 00:08:55,390 --> 00:08:59,890 because visually it just breaks up the math into its component parts. 128 00:08:59,890 --> 00:09:01,770 So, we know what's going to happen first. 129 00:09:01,770 --> 00:09:03,810 It certainly doesn't hurt to add these extra parentheses. 130 00:09:03,810 --> 00:09:06,500 You're never gonna to see a performance difference in your program. 131 00:09:08,150 --> 00:09:09,900 Just to make sure this all works. 132 00:09:09,900 --> 00:09:12,680 Let's command our run forest run. 133 00:09:13,800 --> 00:09:17,735 And here we go we see a sub total of 36.05, 134 00:09:17,735 --> 00:09:22,620 and a grand total of 38.32, which is five percent greater than that. 135 00:09:23,840 --> 00:09:27,230 Okay, we have some complex logic here. 136 00:09:27,230 --> 00:09:29,550 Let's go through it all this is a good habit to get into. 137 00:09:30,600 --> 00:09:34,850 So what we have is some logic to determine the ticket price 138 00:09:34,850 --> 00:09:37,816 based on the boules that we declared up top. 139 00:09:37,816 --> 00:09:43,380 Assuming those Boules, like matinĂ©e, won't change for a single transaction, 140 00:09:43,380 --> 00:09:48,056 we can put all of the complex logic inside the for in loop, which we do right here. 141 00:09:48,056 --> 00:09:54,850 That for in loop can take an ar ray of ages, which we declare up here. 142 00:09:54,850 --> 00:09:57,980 At the start of the loop, we take the customerAge, and 143 00:09:57,980 --> 00:10:03,520 we set it equal to the intValue of the age for that iteration of the loop. 144 00:10:05,140 --> 00:10:08,950 At the bottom of the loop, we add the customerPrice for 145 00:10:08,950 --> 00:10:11,820 that iteration to the running subtotal. 146 00:10:11,820 --> 00:10:17,520 You can do that since the scope of the subtotal extends beyond the loop. 147 00:10:18,820 --> 00:10:23,590 Once the loop completes as many times as there are ages, it's done running, and 148 00:10:23,590 --> 00:10:28,750 we proceed to this line down here where we calculate the tax just once. 149 00:10:30,590 --> 00:10:33,780 I hope you found your first trip through the for in loop a satisfying one. 150 00:10:33,780 --> 00:10:35,650 It's a great tool to have at the ready, 151 00:10:35,650 --> 00:10:38,030 especially once you start dealing with more and more collections.