1 00:00:07,370 --> 00:00:09,760 TOMMY MACWILLIAM: Let's talk about loops, one of the common 2 00:00:09,760 --> 00:00:13,380 programming constructs we'll see throughout CS50. 3 00:00:13,380 --> 00:00:15,570 We'll use loops when we want to perform a 4 00:00:15,570 --> 00:00:17,200 task more than once. 5 00:00:17,200 --> 00:00:20,700 For example, we might want to print "hi" 100 times or print 6 00:00:20,700 --> 00:00:22,700 out all the letters of the alphabet. 7 00:00:22,700 --> 00:00:26,030 In both of these cases, we have one thing we want to do 8 00:00:26,030 --> 00:00:28,700 multiple times, print something out. 9 00:00:28,700 --> 00:00:31,970 However, what we print out each time can vary. 10 00:00:31,970 --> 00:00:34,530 So we can do something slightly different on each 11 00:00:34,530 --> 00:00:37,820 iteration, or run through, of the loop. 12 00:00:37,820 --> 00:00:41,280 We'll see three different types of loops in C, while 13 00:00:41,280 --> 00:00:44,700 loops, for loops, and do while loops. 14 00:00:44,700 --> 00:00:47,850 Though these three types of loops have a different syntax, 15 00:00:47,850 --> 00:00:50,630 the ideas behind them are the same. 16 00:00:50,630 --> 00:00:53,930 We'll define some block of code within curly braces, 17 00:00:53,930 --> 00:00:57,810 called the body of the loop, that we want to be executed 18 00:00:57,810 --> 00:00:59,790 some number of times. 19 00:00:59,790 --> 00:01:03,370 By changing the values of variables used in the body, we 20 00:01:03,370 --> 00:01:07,570 can make our loop do something different each time it's run. 21 00:01:07,570 --> 00:01:10,750 With any loop we write, we'll also need to decide when the 22 00:01:10,750 --> 00:01:12,400 loop will stop running. 23 00:01:12,400 --> 00:01:15,650 If we don't do that, then our trusty computer will continue 24 00:01:15,650 --> 00:01:19,040 to run that loop until we kill the program. 25 00:01:19,040 --> 00:01:22,490 In Scratch, we could use the repeat n times 26 00:01:22,490 --> 00:01:24,600 block to create a loop. 27 00:01:24,600 --> 00:01:28,200 All of the pieces inside a block that said repeat 10 28 00:01:28,200 --> 00:01:30,130 would be run 10 times. 29 00:01:30,130 --> 00:01:34,160 And then, we'd move on to the pieces after that loop. 30 00:01:34,160 --> 00:01:38,140 So our stop condition was simply, this block has been 31 00:01:38,140 --> 00:01:39,830 run 10 times. 32 00:01:39,830 --> 00:01:43,900 So let's recreate this in C. In order for Scratch to ensure 33 00:01:43,900 --> 00:01:46,920 that the pieces within the repeat block are executed 34 00:01:46,920 --> 00:01:51,180 exactly 10 times, Scratch needs to keep track of each 35 00:01:51,180 --> 00:01:53,750 execution of the repeat block. 36 00:01:53,750 --> 00:01:56,790 To keep track of how many times our loop body has been 37 00:01:56,790 --> 00:02:00,360 executed, let's create a variable called i. 38 00:02:00,360 --> 00:02:03,400 We'll start i off at zero, since our loop 39 00:02:03,400 --> 00:02:04,650 hasn't been run yet. 40 00:02:10,009 --> 00:02:10,949 OK 41 00:02:10,949 --> 00:02:15,560 Now we'll use the while keyword to start off our loop. 42 00:02:20,100 --> 00:02:24,180 Now we'll need to figure out when our loop will stop, but 43 00:02:24,180 --> 00:02:26,730 let's leave this for now and then come back to it. 44 00:02:30,970 --> 00:02:32,150 All right. 45 00:02:32,150 --> 00:02:35,260 Inside of our loop, let's just print a message out, like 46 00:02:35,260 --> 00:02:38,535 "hi." We can use the printf function for this. 47 00:02:48,400 --> 00:02:49,230 All right. 48 00:02:49,230 --> 00:02:54,210 So now we'll record that an iteration of the loop body has 49 00:02:54,210 --> 00:02:55,750 been executed. 50 00:02:55,750 --> 00:03:00,320 We can do that by adding 1 to, or incrementing, our counter 51 00:03:00,320 --> 00:03:01,740 variable, i. 52 00:03:01,740 --> 00:03:09,180 To do that, we can say i is equal to i plus 1, or more 53 00:03:09,180 --> 00:03:16,520 simply, i plus plus. 54 00:03:16,520 --> 00:03:17,320 Great. 55 00:03:17,320 --> 00:03:21,810 So now we can see that each time our loop is run, our 56 00:03:21,810 --> 00:03:24,500 counter variable goes up by one. 57 00:03:24,500 --> 00:03:29,410 So we know exactly how many iterations we've run so far. 58 00:03:29,410 --> 00:03:31,630 After one iteration of the loop, our value 59 00:03:31,630 --> 00:03:34,270 of i will be 1. 60 00:03:34,270 --> 00:03:37,460 After two iterations, i will be 2. 61 00:03:37,460 --> 00:03:42,640 And after 10 iterations, i will be 10. 62 00:03:42,640 --> 00:03:46,780 So, if we want to run this loop exactly 10 times, then 63 00:03:46,780 --> 00:03:49,240 this is when we want to stop. 64 00:03:49,240 --> 00:03:53,860 So we want to run this loop while i is less than 10, and 65 00:03:53,860 --> 00:03:57,630 that's exactly what we'll write, while i 66 00:03:57,630 --> 00:04:02,450 is less than 10. 67 00:04:02,450 --> 00:04:06,380 This condition looks just like the conditions we used in if 68 00:04:06,380 --> 00:04:07,830 else blocks. 69 00:04:07,830 --> 00:04:11,690 After the body of our loop has been executed, our program 70 00:04:11,690 --> 00:04:14,690 will jump back up to the loops condition. 71 00:04:14,690 --> 00:04:17,290 If the condition is true, then the body of the 72 00:04:17,290 --> 00:04:18,990 loop will be run again. 73 00:04:18,990 --> 00:04:22,190 If the condition is no longer true, then our loop won't be 74 00:04:22,190 --> 00:04:25,700 run anymore and will move on to the next line of code 75 00:04:25,700 --> 00:04:27,876 underneath our loop. 76 00:04:27,876 --> 00:04:28,550 All right. 77 00:04:28,550 --> 00:04:30,740 So let's take a look at a second type of 78 00:04:30,740 --> 00:04:33,000 loop, the for loop. 79 00:04:33,000 --> 00:04:37,310 Next to the while keyword, in parentheses, we had one thing, 80 00:04:37,310 --> 00:04:39,840 the condition that needed to be true for the 81 00:04:39,840 --> 00:04:42,010 loop body to be run. 82 00:04:42,010 --> 00:04:45,440 That means we had to create our counter variable outside 83 00:04:45,440 --> 00:04:48,760 of the loop and remember to increment it at some point 84 00:04:48,760 --> 00:04:51,050 inside of the loop. 85 00:04:51,050 --> 00:04:54,320 The header for our for loop, on the other hand, has three 86 00:04:54,320 --> 00:04:58,510 parts, each of which will be separated with a semicolon. 87 00:04:58,510 --> 00:05:01,950 In our first third, we can declare any counter or helper 88 00:05:01,950 --> 00:05:05,000 variables we'd like to use in our loop. 89 00:05:05,000 --> 00:05:07,350 In practice, this can be really helpful. 90 00:05:07,350 --> 00:05:10,590 We really don't need that variable, i, after our while 91 00:05:10,590 --> 00:05:13,650 loop has run, so we really shouldn't have to declare it 92 00:05:13,650 --> 00:05:15,810 outside of the loop. 93 00:05:15,810 --> 00:05:19,080 The second third will be the condition that must be true 94 00:05:19,080 --> 00:05:21,850 for the body to be executed again, just like 95 00:05:21,850 --> 00:05:23,330 in our while loop. 96 00:05:23,330 --> 00:05:26,350 In our last third, we can run a statement that will be 97 00:05:26,350 --> 00:05:30,270 executed after each iteration of the loop, so we don't have 98 00:05:30,270 --> 00:05:32,710 to build it into the loop body. 99 00:05:32,710 --> 00:05:38,390 So let's write a for loop that counts down from 5 to 1. 100 00:05:38,390 --> 00:05:41,790 We'll start with the for keyword. 101 00:05:41,790 --> 00:05:44,680 We can create a counter variable first, which we'll 102 00:05:44,680 --> 00:05:47,840 set to 5 this time since we're counting down, 103 00:05:47,840 --> 00:05:50,110 followed by a semicolon. 104 00:05:50,110 --> 00:05:53,660 Next is our condition, which we'll come back to. 105 00:05:53,660 --> 00:05:57,540 Third, we'd like to decrement our counter variable after 106 00:05:57,540 --> 00:05:59,620 each iteration of the loop. 107 00:05:59,620 --> 00:06:04,740 So rather than saying i plus plus, we'll say i minus minus. 108 00:06:04,740 --> 00:06:05,300 All right. 109 00:06:05,300 --> 00:06:09,490 So we want the loop body to run while i is still 110 00:06:09,490 --> 00:06:11,960 greater than 0. 111 00:06:11,960 --> 00:06:16,160 In the body of the loop, let's print out the value of i. 112 00:06:16,160 --> 00:06:20,480 To do so, we'll use the printf function, using the %d 113 00:06:20,480 --> 00:06:21,650 placeholder. 114 00:06:21,650 --> 00:06:24,820 Remember, that placeholder will be replaced with the 115 00:06:24,820 --> 00:06:27,040 value of i. 116 00:06:27,040 --> 00:06:32,060 Finally, let's add a statement after our for loop. 117 00:06:32,060 --> 00:06:35,850 When we run this loop, i will start off at 5, 118 00:06:35,850 --> 00:06:38,070 so 5 will be printed. 119 00:06:38,070 --> 00:06:42,000 Once i gets to 0, the continuation condition, i is 120 00:06:42,000 --> 00:06:44,450 greater than 0, will no longer hold. 121 00:06:44,450 --> 00:06:47,330 So our loop will stop executing, and we'll see the 122 00:06:47,330 --> 00:06:49,850 statement after the loop. 123 00:06:49,850 --> 00:06:51,340 So let's run this code. 124 00:06:51,340 --> 00:06:52,700 First, we'll compile a 125 00:06:52,700 --> 00:06:56,460 countdown.c with make countdown. 126 00:06:56,460 --> 00:06:59,320 Now, we can run this code with ./countdown. 127 00:07:02,340 --> 00:07:06,040 In both while loops and for loops, our continuation 128 00:07:06,040 --> 00:07:09,010 condition will be checked before the body 129 00:07:09,010 --> 00:07:10,960 of the loop is executed. 130 00:07:10,960 --> 00:07:13,840 That means that, if our condition is not initially 131 00:07:13,840 --> 00:07:18,100 true, then the body of our loop will never be run. 132 00:07:18,100 --> 00:07:22,060 So it's sometimes useful to check the condition after the 133 00:07:22,060 --> 00:07:25,150 body of the loop rather than before it. 134 00:07:25,150 --> 00:07:28,270 So let's write a loop to prompt the user for a number 135 00:07:28,270 --> 00:07:30,990 until a positive number is supplied. 136 00:07:30,990 --> 00:07:34,740 If the user inputs a negative number, we'll want to ask them 137 00:07:34,740 --> 00:07:36,260 for another number. 138 00:07:36,260 --> 00:07:38,940 So we'll want this prompt to be inside the 139 00:07:38,940 --> 00:07:41,050 body off the loop. 140 00:07:41,050 --> 00:07:44,730 However, when the loop is run for the first time, the user 141 00:07:44,730 --> 00:07:46,750 hasn't given us the number yet. 142 00:07:46,750 --> 00:07:49,640 So it doesn't make sense to check if it's positive. 143 00:07:49,640 --> 00:07:54,020 Instead, we'll want to check the number after the body of 144 00:07:54,020 --> 00:07:55,720 the loop is run. 145 00:07:55,720 --> 00:08:00,310 We can do this with a do while loop. 146 00:08:00,310 --> 00:08:03,560 First, we'll create a variable, n, that will hold 147 00:08:03,560 --> 00:08:05,230 the user's input. 148 00:08:05,230 --> 00:08:09,960 Now we'll use the do keyword, followed by curly braces that 149 00:08:09,960 --> 00:08:12,930 will start off the body of our loop. 150 00:08:12,930 --> 00:08:16,230 In the body, we can prompt the user for a number with the 151 00:08:16,230 --> 00:08:18,480 GetInt function. 152 00:08:18,480 --> 00:08:23,230 Now, we'll want the body of this loop to execute again if 153 00:08:23,230 --> 00:08:28,370 the user typed a negative number, so we'll say while n 154 00:08:28,370 --> 00:08:30,420 is less than 0. 155 00:08:30,420 --> 00:08:33,140 Notice the semicolon here after the while statement. 156 00:08:38,909 --> 00:08:40,679 So let's run this code. 157 00:08:40,679 --> 00:08:44,780 First, we'll compile this with make positive. 158 00:08:44,780 --> 00:08:49,340 Now we can run the program with ./positive. 159 00:08:49,340 --> 00:08:52,660 If we give this program a negative number, like negative 160 00:08:52,660 --> 00:08:56,560 4, then we'll be prompted again for a number since the 161 00:08:56,560 --> 00:09:00,490 condition of our do while loop was true. 162 00:09:00,490 --> 00:09:04,690 Once we give a positive number, like 8, then the 163 00:09:04,690 --> 00:09:08,560 condition of our do while loop will no longer be true. 164 00:09:08,560 --> 00:09:12,080 So the loop will not be executed again. 165 00:09:12,080 --> 00:09:15,290 And that's it for the three types of loops we'll use in C. 166 00:09:15,290 --> 00:09:18,840 My name is Tommy, and this is CS50.