1 00:00:08,631 --> 00:00:10,000 Welcome back. 2 00:00:10,000 --> 00:00:11,090 So you've already seen the for 3 00:00:11,090 --> 00:00:14,180 loop as a way to iterate over every item in a sequence. 4 00:00:14,180 --> 00:00:17,000 A while loop is a much more general way of iterating. 5 00:00:18,430 --> 00:00:23,130 So a while loop is kind of like a hybrid between a for statement and 6 00:00:23,130 --> 00:00:24,810 an if statement. 7 00:00:24,810 --> 00:00:26,590 So a while loop looks like this. 8 00:00:26,590 --> 00:00:29,880 You say while, and then just like an if statement, 9 00:00:29,880 --> 00:00:32,370 you have a conditional that comes after the while. 10 00:00:33,450 --> 00:00:40,000 And then you have a piece of code that will run if this condition is true. 11 00:00:40,000 --> 00:00:44,540 But unlike in if statement where after you're done running this piece of code, 12 00:00:44,540 --> 00:00:46,530 if you use a while instead. 13 00:00:46,530 --> 00:00:50,960 Then by the time you get to this end of code, then we loop back up here and 14 00:00:50,960 --> 00:00:54,060 check now is this condition is still true. 15 00:00:54,060 --> 00:00:57,638 So in the sense that it loops back up to the top, that's kind of like a for 16 00:00:57,638 --> 00:00:58,303 statement. 17 00:00:58,303 --> 00:01:02,152 So again, when we get to a while statement, we check, 18 00:01:02,152 --> 00:01:04,540 is this condition true? 19 00:01:04,540 --> 00:01:08,090 If the condition is true, then we run this piece of code and 20 00:01:08,090 --> 00:01:12,610 then we go back up and check is this condition still true. 21 00:01:12,610 --> 00:01:16,805 So, if this condition is not true, just like in if statement, 22 00:01:16,805 --> 00:01:20,690 then we skip this block of code and execute the next code block. 23 00:01:21,900 --> 00:01:24,070 So, let's see this in action. 24 00:01:27,226 --> 00:01:32,538 So, here I have some code that's going to take the sum of numbers one plus two, 25 00:01:32,538 --> 00:01:36,300 plus three, plus whatever, and we pass in. 26 00:01:36,300 --> 00:01:43,150 So here, we pass in a number called aBound as an argument to our sumTo function. 27 00:01:44,730 --> 00:01:47,690 And we're using a while loop in order to do this. 28 00:01:49,990 --> 00:01:54,503 So on line 11, we print out the value of sumTo(4), 29 00:01:54,503 --> 00:02:01,334 which should be one plus two, plus three, plus four, which is going to give us ten. 30 00:02:02,810 --> 00:02:04,870 And then here we print out the sumTo(1000), 31 00:02:04,870 --> 00:02:08,510 which is going to be a much larger number. 32 00:02:08,510 --> 00:02:10,300 So first I'm just going to run my code, 33 00:02:10,300 --> 00:02:12,240 just to make sure that it still works correctly. 34 00:02:13,430 --> 00:02:19,827 I'm good, so I see I get I get ten here, and I get this number from line 13. 35 00:02:19,827 --> 00:02:24,290 Okay, so now that we've done that, let's look a little bit at how it works. 36 00:02:24,290 --> 00:02:28,500 So, you see that we initialized two variables here. 37 00:02:28,500 --> 00:02:31,604 So we start up this sum to be zero, and 38 00:02:31,604 --> 00:02:35,390 this is going to be kind of a kin to our accumulator variable. 39 00:02:37,560 --> 00:02:40,473 So, what it's going to keep track of is theSum. 40 00:02:43,778 --> 00:02:44,598 So far. 41 00:02:49,389 --> 00:02:53,970 And then we have this other variable, which we call aNumber. 42 00:02:53,970 --> 00:02:57,197 And what this is going to keep track of is where we are. 43 00:03:02,090 --> 00:03:07,689 And that's going to start out as one, but then we're going to set it to two, 44 00:03:07,689 --> 00:03:12,958 to three, and then so on until we get to n, which in this case is aBound. 45 00:03:16,955 --> 00:03:21,080 So now here we have a while statement. 46 00:03:21,080 --> 00:03:26,581 And our condition is while aNumber is less than or equal to aBound, 47 00:03:26,581 --> 00:03:30,820 again aBound here is whatever we're adding up to. 48 00:03:31,830 --> 00:03:37,120 So as long as our aNumber variable is less than or equal to aBound. 49 00:03:37,120 --> 00:03:44,550 Then what we do is we first say, theSum equals its previous value plus a number. 50 00:03:44,550 --> 00:03:51,079 So when a number starts out as one, then theSum is going to be zero plus one. 51 00:03:51,079 --> 00:03:53,816 Just going to be one. 52 00:03:53,816 --> 00:04:00,020 So then after that, what we do is we say aNumber equals aNumber plus one. 53 00:04:00,020 --> 00:04:02,880 Again, aNumber keeps track of where we are. 54 00:04:02,880 --> 00:04:07,239 So aNumber is going to start out as one, and 55 00:04:07,239 --> 00:04:13,054 then it's going to two, and then three, and then four, 56 00:04:13,054 --> 00:04:18,647 and so on until aNumber is less than or equal to aBound. 57 00:04:18,647 --> 00:04:23,010 And again, aBound here is the number that we're adding up to. 58 00:04:23,010 --> 00:04:28,213 So it's going to keep going up by one until we get to aBound. 59 00:04:34,326 --> 00:04:38,104 And as we're doing that, as we're incrementing this aNumber, 60 00:04:38,104 --> 00:04:41,620 then we're adding that number to theSum that we have so far. 61 00:04:42,850 --> 00:04:45,918 And then by the time this while loop is done running, 62 00:04:45,918 --> 00:04:49,080 theSum is going to have the correct answer. 63 00:04:49,080 --> 00:04:53,420 But let's inspect this code just to make it a little bit more clear. 64 00:04:53,420 --> 00:04:57,450 So I'm going to open up code lines, and here I have the same piece of code, 65 00:04:57,450 --> 00:05:01,150 except I'm only printing out sumTo when called with four. 66 00:05:02,380 --> 00:05:06,060 So, the first thing that we're going to do is evaluate the function, so 67 00:05:06,060 --> 00:05:11,160 we can see that sumTo is the function that we declared right here. 68 00:05:11,160 --> 00:05:16,596 And then we print out the value of sumTo when called with four. 69 00:05:16,596 --> 00:05:20,320 So, what the means is that aBound is going to have the value of four. 70 00:05:21,810 --> 00:05:26,610 And then we start out the theSum with value zero, and aNumber with one. 71 00:05:26,610 --> 00:05:30,452 Remember, theSum keeps track of the sum so far. 72 00:05:33,933 --> 00:05:37,192 And aNumber keeps track of where we are. 73 00:05:42,173 --> 00:05:45,420 And now we're at the important bit, the while loop. 74 00:05:45,420 --> 00:05:51,030 Again, we say while aNumber is less than or equal to aBound. 75 00:05:51,030 --> 00:05:54,376 The first time that we run this, aNumber has the value one. 76 00:05:54,376 --> 00:06:00,210 One is less than or equal to aBound, which is four, and so yes, we do run this code. 77 00:06:01,260 --> 00:06:06,720 So in this code, we say theSum equals this sum plus aNumber. 78 00:06:06,720 --> 00:06:10,317 So theSum is going to go from zero to one. 79 00:06:10,317 --> 00:06:16,891 Then we say aNumber is aNumber plus one, so it goes from one to two. 80 00:06:16,891 --> 00:06:20,154 Now, the next time we run this, we check, 81 00:06:20,154 --> 00:06:23,970 is aNumber still less than or equal to aBound. 82 00:06:23,970 --> 00:06:28,362 So in this case, aNumber is now two. 83 00:06:28,362 --> 00:06:31,240 And we ask, is two less than or equal to four? 84 00:06:31,240 --> 00:06:32,320 Yes it is. 85 00:06:32,320 --> 00:06:37,650 And so we run the code in here, and we save theSum equals theSum plus aNumber. 86 00:06:37,650 --> 00:06:44,420 aNumber is now two, so theSum is going to go from one, two, three. 87 00:06:44,420 --> 00:06:49,230 So theSum is now three, and then aNumber gets its previous 88 00:06:49,230 --> 00:06:53,955 value plus one, so aNumber is going to from two to three. 89 00:06:53,955 --> 00:06:58,470 And we ask again, is aNumber less than or equal to aBound? 90 00:06:58,470 --> 00:07:01,660 Yes, three is less than or equal to four so we run this code. 91 00:07:01,660 --> 00:07:06,607 So theSum goes from three to three plus aNumber which is three. 92 00:07:06,607 --> 00:07:13,441 So theSum goes to six, and aNumber is going to from three to four. 93 00:07:13,441 --> 00:07:16,420 And then we ask, is four less than or equal to four? 94 00:07:16,420 --> 00:07:22,010 Yes, it is, so theSum is going to go from six to six plus four, or ten. 95 00:07:23,150 --> 00:07:26,924 And aNumber is going to go from four to four plus one or five. 96 00:07:26,924 --> 00:07:31,794 And now here's another key point. 97 00:07:31,794 --> 00:07:33,500 So now, aNumber is five. 98 00:07:33,500 --> 00:07:37,542 And when we ask is five less than or equal to four? 99 00:07:37,542 --> 00:07:40,539 It is not, so this condition is false. 100 00:07:42,596 --> 00:07:45,140 Meaning that we're done running our while loop. 101 00:07:46,350 --> 00:07:50,270 And we can see that the sum here has the value that we actually want. 102 00:07:51,620 --> 00:07:55,270 So now, when we're done running our while, we skip to line nine, 103 00:07:55,270 --> 00:07:59,690 which returns theSum, and we get ten, which is the correct answer. 104 00:08:00,910 --> 00:08:03,030 So let's answer a few questions. 105 00:08:03,030 --> 00:08:08,200 First, true or false, you can rewrite any for loop as a while loop. 106 00:08:08,200 --> 00:08:09,370 Well, this is true. 107 00:08:09,370 --> 00:08:12,640 A while loop is a much more general form of iteration. 108 00:08:12,640 --> 00:08:15,820 It's capable of expressing what you can express in a for loop and 109 00:08:15,820 --> 00:08:17,060 more, as we'll see in a bit. 110 00:08:18,360 --> 00:08:19,620 This question asks, 111 00:08:19,620 --> 00:08:23,160 which type of loop can be used to perform the following iteration? 112 00:08:23,160 --> 00:08:25,190 You choose a positive integer at random, and 113 00:08:25,190 --> 00:08:30,040 then print out the numbers one up to and including the selected integer. 114 00:08:30,040 --> 00:08:32,340 In this case, we could actually use a for 115 00:08:32,340 --> 00:08:36,220 loop if we wanted to, because we could use the range function. 116 00:08:36,220 --> 00:08:39,910 And anytime you can use a for loop, you could also use a while loop. 117 00:08:39,910 --> 00:08:42,442 So I'm going to say here that the answer is A. 118 00:08:44,626 --> 00:08:49,275 In this question, we're asked to write a while loop that's initialized to zero and 119 00:08:49,275 --> 00:08:52,450 stops at 15 if the counter is an even number. 120 00:08:52,450 --> 00:08:55,155 Append the counter to a list called eve_nums. 121 00:08:57,030 --> 00:09:03,855 So, I'm going to say count equals zero, and I want it to stop at 15, 122 00:09:03,855 --> 00:09:09,093 so I'll say while count is less than or equal to 15. 123 00:09:11,878 --> 00:09:15,534 And I'm just going to say count equals count plus one. 124 00:09:19,027 --> 00:09:23,840 So, what we're doing in this code is we're initializing count to be zero. 125 00:09:23,840 --> 00:09:28,530 And then inside of the while loop, as long as count is less than 15, 126 00:09:28,530 --> 00:09:33,350 we assign it to value to its previous value plus one. 127 00:09:33,350 --> 00:09:37,569 So count is going to go from zero to one, to two, to three, four, five, 128 00:09:37,569 --> 00:09:42,376 six, seven, eight, nine, ten, 11, 12, 13, 14, and then 15. 129 00:09:42,376 --> 00:09:47,360 And then it does get assigned to 16. 130 00:09:47,360 --> 00:09:52,187 But as soon as it's 16, then we exit this while loop, 131 00:09:52,187 --> 00:09:56,216 because 16 is not less than or equal to 15. 132 00:09:56,216 --> 00:10:02,189 And so by the time we're done with this while loop, count is going to be 16. 133 00:10:02,189 --> 00:10:06,090 Now this question is asking us to do a little bit more, it's saying if 134 00:10:06,090 --> 00:10:10,515 the counter is an even number, append the counter to list called eve_nums. 135 00:10:10,515 --> 00:10:15,621 So I can check if the counter is an even number 136 00:10:15,621 --> 00:10:20,037 by saying if count modulo two is zero. 137 00:10:20,037 --> 00:10:24,200 Again that's just saying if the remainder when divided by two is zero. 138 00:10:24,200 --> 00:10:28,808 And what we want to do then is we want to append it to this list 139 00:10:28,808 --> 00:10:31,215 which I'll call eve_nums. 140 00:10:32,350 --> 00:10:34,480 I mean, to start out as an empty list and 141 00:10:34,480 --> 00:10:38,420 if count is even, I want to say eve_nums.append count. 142 00:10:42,690 --> 00:10:45,385 So let's test our code to be sure that it works. 143 00:10:48,649 --> 00:10:52,810 Okay, so we can see that now our code passes all the tasks. 144 00:10:52,810 --> 00:10:57,418 And even_nums has the values zero, two, 145 00:10:57,418 --> 00:11:01,643 four, six, eight, and so on to 14. 146 00:11:01,643 --> 00:11:04,615 This question says, below we've provided a for 147 00:11:04,615 --> 00:11:07,380 loop that sums all of the elements of list1. 148 00:11:07,380 --> 00:11:12,420 Write code that accomplishes the same tasks but instead uses a while loop. 149 00:11:12,420 --> 00:11:15,960 Assign the accumulator variable to the name accum. 150 00:11:15,960 --> 00:11:19,450 So the strategy that we're going to take in order to do this 151 00:11:19,450 --> 00:11:24,040 is we're going to have one variable that's going to keep track of the current index. 152 00:11:24,040 --> 00:11:26,140 So I'm going to call that variable idx. 153 00:11:28,580 --> 00:11:35,343 idx is going to be zero, and then one, and then two, three, four, five, six. 154 00:11:35,343 --> 00:11:40,980 And then by the time it's seven, we want it to stop because there isn't a 7th item. 155 00:11:42,500 --> 00:11:45,860 So let's write the code to properly set idx first. 156 00:11:45,860 --> 00:11:51,754 I'm going to say IDX equals zero, 157 00:11:51,754 --> 00:11:56,835 and I'll say while idx is less 158 00:11:56,835 --> 00:12:01,510 than the length of list1, 159 00:12:01,510 --> 00:12:06,604 then idx equals idx plus one. 160 00:12:07,990 --> 00:12:12,650 Now an important point here is that I said while idx is less than 161 00:12:12,650 --> 00:12:16,150 the length of list1, and I initialized it to zero. 162 00:12:16,150 --> 00:12:18,600 So why did I do both of these things? 163 00:12:18,600 --> 00:12:22,308 Well, first I initialized it to zero because again list and 164 00:12:22,308 --> 00:12:24,400 sequences are zero indexed. 165 00:12:24,400 --> 00:12:28,460 Meaning that we have to start out at zero to get the first item. 166 00:12:28,460 --> 00:12:33,664 Now, I said less then the length of this one, rather than less than or equal to, 167 00:12:33,664 --> 00:12:39,700 because here this one is one, two, three, four, five, six, seven items long. 168 00:12:39,700 --> 00:12:42,314 So the length of this one is seven. 169 00:12:42,314 --> 00:12:46,410 But we only want to go until the last item. 170 00:12:46,410 --> 00:12:52,614 And because we're zero index, we have the last item is actually item six. 171 00:12:52,614 --> 00:12:58,290 So by the time we get to idx equals seven, then we want to breakout of this loop. 172 00:12:58,290 --> 00:13:02,461 We only want idx to be zero, one, two, three, four, five, six. 173 00:13:02,461 --> 00:13:06,810 So I'm going to say less than the length of this list, not less than or equal to. 174 00:13:08,280 --> 00:13:13,070 Okay, now we don't just want to loop through all of the indices of this list. 175 00:13:13,070 --> 00:13:19,640 We also want to assign a new variable accum to be the sum of every item. 176 00:13:19,640 --> 00:13:24,110 So I'm going to start out accum equals zero, 177 00:13:24,110 --> 00:13:27,580 to say that we haven't seen anything so far. 178 00:13:27,580 --> 00:13:31,910 And then just like on line six here where we say total equals total 179 00:13:31,910 --> 00:13:33,650 plus the value of the element. 180 00:13:33,650 --> 00:13:38,600 I want to say accum equals accum, and 181 00:13:38,600 --> 00:13:45,320 then I'm want to say, plus the value at index idx. 182 00:13:45,320 --> 00:13:51,569 And I get that by saying list1 sub idx. 183 00:13:53,416 --> 00:13:57,890 So when idx is zero, this is going to be eight. 184 00:13:57,890 --> 00:14:05,050 When idx is one, this is going to be three and so on. 185 00:14:05,050 --> 00:14:09,450 So again, as idx iterates through all of the indices. 186 00:14:09,450 --> 00:14:14,410 This is going to be the value at that index, and 187 00:14:14,410 --> 00:14:19,770 so we add the value at that index to the previously accumulated value. 188 00:14:19,770 --> 00:14:22,020 Now let's run our code and be sure that it's correct. 189 00:14:24,220 --> 00:14:27,430 So we can see that our code works as we wanted it to. 190 00:14:29,000 --> 00:14:30,730 That's all for now, until next time.