1 00:00:00,320 --> 00:00:02,089 So far, we've met the for loop and 2 00:00:02,089 --> 00:00:05,087 have seen how it can be used in the BoxOffice Plus project. 3 00:00:05,087 --> 00:00:07,483 I hope the example opened your eyes a bit to how for 4 00:00:07,483 --> 00:00:10,080 loops can be used in all sorts of scenarios. 5 00:00:10,080 --> 00:00:14,650 In this lesson, we'll meet the last two flavors of loops, the while loop and 6 00:00:14,650 --> 00:00:18,100 the do while loop, which are closely related to each other. 7 00:00:18,100 --> 00:00:21,840 We'll also see how adding a break like we saw in our switch statement 8 00:00:21,840 --> 00:00:23,610 can help make our loops more efficient. 9 00:00:24,850 --> 00:00:27,760 I've known I've thrown a lot of code and syntax at you already. 10 00:00:27,760 --> 00:00:31,630 So for the while loops, we'll try to approach them a bit more abstractly, and 11 00:00:31,630 --> 00:00:35,410 we won't worry about shoehorning them into our existing Box Office code. 12 00:00:35,410 --> 00:00:39,190 You should end this lesson with an understanding of how while loops work and 13 00:00:39,190 --> 00:00:41,160 where you might wanna use them in the future. 14 00:00:41,160 --> 00:00:44,170 And that should be plenty to continue along in our Objective-C track. 15 00:00:45,820 --> 00:00:51,310 So a while loop, like all loops, runs, or doesn't run, based on a condition. 16 00:00:51,310 --> 00:00:53,710 As the name suggests with a while loop, 17 00:00:53,710 --> 00:00:56,540 we will run the loop while the condition is true. 18 00:00:57,735 --> 00:01:00,875 For example, let's say we're building a video game. 19 00:01:00,875 --> 00:01:03,915 In our video game, there's an evil monster you have to fight. 20 00:01:03,915 --> 00:01:07,715 When he's alive and on screen, we wanna display lots of scary lightning bolts and 21 00:01:07,715 --> 00:01:09,927 send ghosts moving around in the background. 22 00:01:09,927 --> 00:01:12,892 If we managed to defeat the evil monster, the sky should clear, and 23 00:01:12,892 --> 00:01:14,772 the ghost should disappear. 24 00:01:14,772 --> 00:01:18,712 Now in our code, it might make sense to place the code for displaying 25 00:01:18,712 --> 00:01:23,054 the lightning and ghosts inside a while loop that says something like this. 26 00:01:23,054 --> 00:01:28,456 While (monsterIsAlive){[displayGhostsAndLightni- 27 00:01:28,456 --> 00:01:32,470 ng]}, that entire loop can be triggered over and over with a timer. 28 00:01:32,470 --> 00:01:36,060 That way as soon as the monster is no longer alive, the ghost and 29 00:01:36,060 --> 00:01:38,135 lightning would stop automatically. 30 00:01:38,135 --> 00:01:42,330 We don't have to actually go find those elements and manually remove them, or 31 00:01:42,330 --> 00:01:45,810 keep a persistent connection to them when they're created. 32 00:01:45,810 --> 00:01:49,440 Let's turn back to our example of a movie theater to put a finer point 33 00:01:49,440 --> 00:01:50,020 on the while loop. 34 00:01:51,200 --> 00:01:52,854 So keeping with our movie theater theme, 35 00:01:52,854 --> 00:01:57,060 let's say that we're adding a promotion where the first 100 customers each day 36 00:01:57,060 --> 00:02:02,390 get a coupon for \$1 off popcorn with a maximum of 3 coupons per group. 37 00:02:02,390 --> 00:02:04,950 A while loop could fit this situation nicely. 38 00:02:04,950 --> 00:02:06,931 It would look something like, 39 00:02:06,931 --> 00:02:12,110 while (ticketsSold <= 100 ) { // print popcorn coupon }. 40 00:02:12,110 --> 00:02:14,910 As long as tickets sold is less than or equal to 100, 41 00:02:14,910 --> 00:02:17,890 print the coupon, if not, skip the loop. 42 00:02:19,030 --> 00:02:23,410 Now assuming the ticket sold variable is persistent from one group of customers to 43 00:02:23,410 --> 00:02:27,670 the next, our running total should track how many tickets are sold. 44 00:02:27,670 --> 00:02:31,100 But how about this maximum of three coupons per group? 45 00:02:31,100 --> 00:02:35,731 Well for that, we could change our while loop to include a running count per group 46 00:02:35,731 --> 00:02:38,810 and when it hits the maximum number of three, break. 47 00:02:40,430 --> 00:02:46,038 Int ticketsInGroup; while (ticketsSold <= 100) { //print 48 00:02:46,038 --> 00:02:53,620 popcorn coupon ticketsInGroup++; if (ticketsInGroup >=3) {break;}}. 49 00:02:53,620 --> 00:02:57,800 First, we create an int variable to hold the number of tickets in the group. 50 00:02:57,800 --> 00:03:01,310 Then, each time we start the loop, we print a coupon and 51 00:03:01,310 --> 00:03:04,440 increment the tickets in group variable by one. 52 00:03:04,440 --> 00:03:08,480 Still inside the loop, if tickets in group reaches three, or 53 00:03:08,480 --> 00:03:11,530 more somehow, we break from the loop. 54 00:03:11,530 --> 00:03:14,160 That means that no more coupons will be printed, 55 00:03:14,160 --> 00:03:17,340 even if the tickets sold is still under 100. 56 00:03:17,340 --> 00:03:21,710 This was a stylized example, but adding breaks can be really important. 57 00:03:21,710 --> 00:03:24,740 Especially when your loop might otherwise run thousands of times and 58 00:03:24,740 --> 00:03:26,820 suck up lots of precious memory and battery life. 59 00:03:27,920 --> 00:03:29,030 As I said a moment ago, 60 00:03:29,030 --> 00:03:32,300 the while loop has a cousin worth mentioning, the do while loop. 61 00:03:32,300 --> 00:03:35,780 The do while loop is very much like the while loop 62 00:03:35,780 --> 00:03:38,100 though they always run at least once. 63 00:03:38,100 --> 00:03:43,588 Their syntax is actually int i = 10; 64 00:03:43,588 --> 00:03:48,420 do { i--;} while (i > 0). 65 00:03:48,420 --> 00:03:52,399 Now in this example, we would iterate through the loop ten times, 66 00:03:52,399 --> 00:03:56,440 counting down from ten to one, then stopping when i reaches zero. 67 00:03:56,440 --> 00:04:01,330 However, if we changed the initial value for i to 0, so 68 00:04:01,330 --> 00:04:07,830 it was i=0; do {i--;} while (i>0), 69 00:04:07,830 --> 00:04:12,510 it would still run once even though i is already not greater than 0. 70 00:04:12,510 --> 00:04:17,700 The do while loop doesn't check its condition until after the loop completes. 71 00:04:17,700 --> 00:04:22,160 Just to be clear, the do while loop will always run at least once, 72 00:04:22,160 --> 00:04:25,570 regardless of the condition you choose, and whether or not it's true. 73 00:04:26,660 --> 00:04:27,990 Over the past many videos, 74 00:04:27,990 --> 00:04:32,060 you've learned a lot of coding in general, and Objective-C in particular. 75 00:04:32,060 --> 00:04:36,060 In doing so, you've also learned quite a bit of good old fashion C. 76 00:04:36,060 --> 00:04:39,381 I hope you've taken the time to complete all the quizzes and co-challenges and 77 00:04:39,381 --> 00:04:42,170 re-watched anything that might have warranted a second viewing. 78 00:04:43,570 --> 00:04:47,220 In the next stage of videos, we're gonna quickly move through some examples which 79 00:04:47,220 --> 00:04:49,390 put what you've learned into action. 80 00:04:49,390 --> 00:04:52,560 The intent of this next stage isn't to teach you anything new, but 81 00:04:52,560 --> 00:04:55,300 rather to bring those tools and concepts together. 82 00:04:55,300 --> 00:04:57,810 And give you some practice at deciphering the logic and 83 00:04:57,810 --> 00:05:01,020 code faster in a more immersive style. 84 00:05:01,020 --> 00:05:02,050 At the end of this stage, 85 00:05:02,050 --> 00:05:06,970 we'll give you just a taste of our next course, object-oriented Objective-C. 86 00:05:06,970 --> 00:05:10,780 Which in my opinion, is more interesting and certainly more conceptual and 87 00:05:10,780 --> 00:05:12,910 powerful than what we've learned up till now. 88 00:05:14,600 --> 00:05:17,380 I say this not to diminish the work you've done so far. 89 00:05:17,380 --> 00:05:20,290 You have covered a lot of ground and should be very proud of 90 00:05:20,290 --> 00:05:24,780 the effort you've put in, but just wait, we are about to really get going. 91 00:05:24,780 --> 00:05:27,590 If your time up until now has been learning about saws, 92 00:05:27,590 --> 00:05:28,950 hammers, wood and glue, 93 00:05:28,950 --> 00:05:32,950 you're about to start learning how to really build something with those tools. 94 00:05:32,950 --> 00:05:35,860 Stay with me and the concepts we'll cover in our next course 95 00:05:35,860 --> 00:05:39,280 will really take the elements you've learned to the next level. 96 00:05:39,280 --> 00:05:44,140 But before that, let's immerse ourselves in one more stage of Objective-C basics. 97 00:05:44,140 --> 00:05:46,350 Practicing with immersive examples. 98 00:05:46,350 --> 00:05:49,850 We'll model a few short real life problems to help solidify what we've 99 00:05:49,850 --> 00:05:51,150 already covered. 100 00:05:51,150 --> 00:05:55,430 I promise, these next few videos won't introduce any new concepts or syntax, but 101 00:05:55,430 --> 00:05:58,010 rather, just aim to reinforce your understanding. 102 00:05:58,010 --> 00:06:02,070 With that in mind, I strongly encourage you to begin that next stage right now. 103 00:06:02,070 --> 00:06:02,670 See you in a minute.