1 00:00:00,460 --> 00:00:03,260 Sometimes, we want to express a range of values so 2 00:00:03,260 --> 00:00:06,200 that we can do things a certain number of times. 3 00:00:06,200 --> 00:00:09,920 For example, let's say I want to play a song on repeat ten times or 4 00:00:09,920 --> 00:00:11,430 I want to swim ten laps. 5 00:00:12,490 --> 00:00:14,010 In both those examples, 6 00:00:14,010 --> 00:00:18,860 the range of values, when repeating that action, is one to ten. 7 00:00:18,860 --> 00:00:21,670 We want to take that many turns and do a certain thing. 8 00:00:23,820 --> 00:00:27,020 Let's add a new comment in here and say, Ranges. 9 00:00:28,160 --> 00:00:33,100 In Swift we can express a range with two kinds of operators. 10 00:00:33,100 --> 00:00:35,740 The first operator is called the closed range operator and 11 00:00:35,740 --> 00:00:41,290 the syntax looks like this, where a and b represent the starting and 12 00:00:41,290 --> 00:00:46,120 ending number, or the upper and lower bounds of the range. 13 00:00:46,120 --> 00:00:52,520 A closed range operator, one that has three periods, dots or full stops whatever 14 00:00:52,520 --> 00:00:58,540 you want to call them, creates a range of values that includes the values a and b. 15 00:00:58,540 --> 00:01:03,740 So if we were to write this, 1 then three dots and then 5, 16 00:01:03,740 --> 00:01:07,340 that would create a range representing the numbers 1, 2, 3, 4, and 5. 17 00:01:07,340 --> 00:01:14,050 The second operator is called a half open range operator. 18 00:01:15,060 --> 00:01:18,620 This operator uses the syntax as follows. 19 00:01:18,620 --> 00:01:23,370 So here we have a and b again, but in between we have two periods or dots. 20 00:01:23,370 --> 00:01:26,240 And then an opening angle bracket and then the b. 21 00:01:27,500 --> 00:01:31,494 Now in this case the range includes a but not b. 22 00:01:31,494 --> 00:01:34,660 It goes up to b minus one. 23 00:01:34,660 --> 00:01:39,954 So if we use this operator and wrote 1 period, period, an angle bracket, 24 00:01:39,954 --> 00:01:45,010 and then 5, we would get the numbers 1, 2, 3, and 4 in our range. 25 00:01:46,390 --> 00:01:50,390 Both these operators can be used in conjunction with a for 26 00:01:50,390 --> 00:01:54,690 in loop to execute whatever code we want a certain number of times. 27 00:01:55,880 --> 00:02:00,130 So, let's say I wanted to print out a multiplication table for 28 00:02:00,130 --> 00:02:03,690 5 or the 5 times table as it's commonly called. 29 00:02:04,770 --> 00:02:06,300 We can write something like this. 30 00:02:06,300 --> 00:02:09,456 We start with the for in loop and this works just like before. 31 00:02:09,456 --> 00:02:11,870 So we'll say for. 32 00:02:11,870 --> 00:02:14,710 Now we're not working with the collection here, but we still want to take 33 00:02:14,710 --> 00:02:19,020 a number from our range and then assign it to a constant and use that. 34 00:02:19,020 --> 00:02:21,960 So we still have to give a name and we'll call it number over here. 35 00:02:23,550 --> 00:02:28,260 We'll see in just like before, and then we'll put a range in here so 36 00:02:28,260 --> 00:02:33,858 we'll say 1, three dots, and then a 10. 37 00:02:33,858 --> 00:02:36,680 With syntax is the same as before, but 38 00:02:36,680 --> 00:02:41,430 instead of iterating through an array, we're iterating through the range. 39 00:02:41,430 --> 00:02:43,260 Now, what do we mean by iterating? 40 00:02:43,260 --> 00:02:46,688 That means going over each value in a collection or 41 00:02:46,688 --> 00:02:49,340 a range and doing something with it. 42 00:02:49,340 --> 00:02:52,910 So each number in the range is assigned to the constant number. 43 00:02:54,610 --> 00:02:57,450 Inside our a loop like before, we can add some code. 44 00:02:57,450 --> 00:03:00,699 In here we'll simply print out the multiplication table. 45 00:03:01,900 --> 00:03:05,630 In the Swift Basics Course, we learned about string interpolation. 46 00:03:07,080 --> 00:03:10,930 We can use string interpolation here to pass the number variable 47 00:03:10,930 --> 00:03:12,750 directly into a string and print it. 48 00:03:13,890 --> 00:03:17,820 So, here we'll say, print and inside we'll create a string. 49 00:03:17,820 --> 00:03:20,450 So we'll start off with an empty string. 50 00:03:20,450 --> 00:03:22,600 Now, of course, this won't print anything. 51 00:03:24,042 --> 00:03:28,766 Inside that string, we'll say some number 52 00:03:28,766 --> 00:03:33,940 times 5 is equal to some other number. 53 00:03:33,940 --> 00:03:38,010 Now you'll see that times 5 is equal to is printed ten times. 54 00:03:38,010 --> 00:03:40,290 One, two, three all the way down. 55 00:03:40,290 --> 00:03:45,120 So this string basically is being executed ten times because here, 56 00:03:45,120 --> 00:03:48,080 we've created a range of 1 through 10. 57 00:03:48,080 --> 00:03:51,560 Now we simply don't want to print this string ten times, 58 00:03:51,560 --> 00:03:53,746 we want to print the 5 times table. 59 00:03:53,746 --> 00:03:55,490 So we'll use string interpolation. 60 00:03:55,490 --> 00:03:58,860 So we'll pass in this number, we'll say number. 61 00:03:58,860 --> 00:04:04,060 Remember the syntax for string interpolation is a backward slash and 62 00:04:04,060 --> 00:04:09,080 a set of parentheses and then inside, we pass in whatever we want to evaluate. 63 00:04:09,080 --> 00:04:14,100 Now you can see that number, every time the loop moves forward is 1, 64 00:04:14,100 --> 00:04:15,470 2, 3, 4, 5 all the way to 10. 65 00:04:15,470 --> 00:04:16,580 Perfect. 66 00:04:16,580 --> 00:04:18,400 So now we can use number. 67 00:04:19,780 --> 00:04:22,930 And inside, again using string interpolation, 68 00:04:22,930 --> 00:04:25,785 we can perform a multiplication operation. 69 00:04:25,785 --> 00:04:28,890 We'll say number times five. 70 00:04:30,500 --> 00:04:35,770 And now you'll see 1 times 5 is equal to 5, 2 times 5 is 10 and so on. 71 00:04:37,630 --> 00:04:42,150 While it might be difficult to think of a concrete example where a for in loop and 72 00:04:42,150 --> 00:04:45,620 a range would come in handy, they do crop up here and there. 73 00:04:45,620 --> 00:04:49,680 So it's useful to know what the syntax is and how it works. 74 00:04:49,680 --> 00:04:53,240 Now, as always, before we jump to the next video, let's take a code challenge.