1 00:00:05,490 --> 00:00:08,299 In this video we'll learn about the c++ while loop. 2 00:00:09,260 --> 00:00:13,159 The while loop is an example of a pre-test loop because the test is 3 00:00:13,160 --> 00:00:14,579 done at the beginning of the loop. 4 00:00:15,139 --> 00:00:18,479 So it's possible that if the test fails immediately, then the 5 00:00:18,480 --> 00:00:20,070 loop body will never execute. 6 00:00:21,230 --> 00:00:24,750 The syntax for the while loop is the keyword while, followed 7 00:00:24,750 --> 00:00:26,689 by an expression in parentheses. 8 00:00:27,200 --> 00:00:29,769 This expression has to evaluate to a Boolean value. 9 00:00:31,090 --> 00:00:34,090 This is followed by the statement or statement blocked to execute if 10 00:00:34,090 --> 00:00:35,719 the expression evaluates to true. 11 00:00:37,160 --> 00:00:40,709 In this slide, we can see a simple while loop that's simply counting. 12 00:00:41,380 --> 00:00:44,140 We have a looping variable named i and we initialize it to 1. 13 00:00:45,389 --> 00:00:48,160 The loop condition is i less than or equal to 5. 14 00:00:48,450 --> 00:00:51,529 So as long as i is less than or equal to 5, we continue 15 00:00:51,549 --> 00:00:52,969 executing the loop's body. 16 00:00:53,699 --> 00:00:56,570 In the body of the loop, we display the variable i 17 00:00:56,630 --> 00:00:57,790 and then increment it by 1. 18 00:00:58,830 --> 00:01:01,940 Eventually, i become 6, and the loop condition fails. 19 00:01:01,940 --> 00:01:03,049 And the loop terminates. 20 00:01:03,889 --> 00:01:06,979 It's important that we increment i in the body of the loop. 21 00:01:07,309 --> 00:01:11,429 If we don't, then the loop will never terminate since i will always be 1, 22 00:01:11,440 --> 00:01:12,969 which is less than or equal to 5. 23 00:01:13,750 --> 00:01:16,600 This is called an infinite loop, and it's not usually what we want. 24 00:01:17,010 --> 00:01:20,270 I'll talk about infinite loops in a later video in this section because 25 00:01:20,270 --> 00:01:21,649 sometimes they can be useful. 26 00:01:22,230 --> 00:01:23,350 Let's see another example. 27 00:01:25,330 --> 00:01:29,166 Let's write a while loop that displays the even numbers between 1 and 10, 28 00:01:29,190 --> 00:01:30,690 just as we did with the for loop. 29 00:01:31,710 --> 00:01:35,540 In this case we, declare i and initialize it to 1 since this is 30 00:01:35,540 --> 00:01:36,760 where we want to start looping. 31 00:01:37,350 --> 00:01:40,830 Then in the loop condition, we'll check to see if i is 32 00:01:40,830 --> 00:01:41,850 less than or equal to 10. 33 00:01:42,330 --> 00:01:46,119 So in other words, we will loop while i is less than or equal to 10. 34 00:01:47,160 --> 00:01:50,140 And then at each iteration, we execute the body of the loop. 35 00:01:50,750 --> 00:01:54,070 In this case, we have an if statement that uses the modulo operator 36 00:01:54,290 --> 00:01:55,590 to see if it's an even number. 37 00:01:56,170 --> 00:01:59,429 If it is, then we display it, then we increment it. 38 00:02:00,410 --> 00:02:04,029 Note that we increment i regardless of whether it's even or not. 39 00:02:04,270 --> 00:02:07,070 Eventually, I becomes 11 and the loop terminates. 40 00:02:08,440 --> 00:02:11,020 Let's see another example of using a while loop with an array. 41 00:02:13,080 --> 00:02:16,330 In this example, we're using a while loop to iterate over an array. 42 00:02:16,990 --> 00:02:18,440 The array is named scores. 43 00:02:18,630 --> 00:02:20,349 And it's an array of three integers. 44 00:02:21,170 --> 00:02:25,340 We set the integer I to 0 since array indexes begin at 0. 45 00:02:26,080 --> 00:02:29,760 And we loop while I is less than three as we've done in other array code. 46 00:02:30,670 --> 00:02:34,470 In the loop body, we display scores sub I then increment I. 47 00:02:35,500 --> 00:02:38,549 When I becomes 3, the condition fails and the loop terminates. 48 00:02:40,230 --> 00:02:43,920 Loops are commonly used in programming to provide input validation. 49 00:02:44,560 --> 00:02:48,060 In this example, we want the user to enter an integer less than a 100. 50 00:02:48,660 --> 00:02:52,090 If they don't, we want to ask them again and again and again 51 00:02:52,090 --> 00:02:53,890 until they enter a valid integer. 52 00:02:54,710 --> 00:02:58,059 So we declare variable name number and initialize it to 0. 53 00:02:58,869 --> 00:03:02,200 Notice now how we prompt the user for the integer and read it from 54 00:03:02,200 --> 00:03:04,480 the console before the loop begins. 55 00:03:05,360 --> 00:03:07,480 Now we have the value they entered in number. 56 00:03:08,120 --> 00:03:09,980 The loop condition is now evaluated. 57 00:03:09,980 --> 00:03:13,530 And if the user's number is not greater than or equal to 100, 58 00:03:13,940 --> 00:03:16,389 then we have a valid number and we never go into the loop. 59 00:03:16,539 --> 00:03:18,740 We simply print the thanks message at the end. 60 00:03:19,560 --> 00:03:22,579 However, if the user enters an integer that is greater than or 61 00:03:22,580 --> 00:03:26,890 equal to 100, we go into the loop and ask for another integer read 62 00:03:26,890 --> 00:03:28,869 it in and then we test again. 63 00:03:29,469 --> 00:03:32,809 This will continue until the user enters a number less than a 100. 64 00:03:33,889 --> 00:03:36,089 Notice that we have some duplicated code here. 65 00:03:36,480 --> 00:03:39,109 We have the same output and input statements outside the 66 00:03:39,110 --> 00:03:40,650 loop and inside the loop. 67 00:03:41,400 --> 00:03:44,130 This is usually a clue that the code could be better. 68 00:03:44,490 --> 00:03:46,630 We'll make this code better in the next video when we 69 00:03:46,630 --> 00:03:48,110 discuss the do while loop. 70 00:03:48,840 --> 00:03:50,600 But this kind of code is commonly seen. 71 00:03:51,450 --> 00:03:52,629 Let's see another example. 72 00:03:53,959 --> 00:03:57,866 In this example, we ask the user to enter a number between 1 and 5. 73 00:03:58,080 --> 00:04:01,800 Notice the code is very similar to our previous example with the exception 74 00:04:01,800 --> 00:04:03,130 of the condition in the loop. 75 00:04:03,849 --> 00:04:05,859 In this case, we're testing both ends. 76 00:04:06,090 --> 00:04:09,150 So if the number is less than or equal to 1, it's not valid. 77 00:04:09,440 --> 00:04:13,340 We also test number greater than equal to 5, which is also invalid. 78 00:04:13,990 --> 00:04:17,529 If either of these conditions is true, then the number is invalid. 79 00:04:17,529 --> 00:04:19,660 So that's why we use the logical or here. 80 00:04:21,899 --> 00:04:25,470 In this example, we'll use a Boolean flag to control our while loop. 81 00:04:25,950 --> 00:04:28,600 First, we declare a Boolean variable named done, and we 82 00:04:28,600 --> 00:04:30,150 set it initially to false. 83 00:04:30,490 --> 00:04:33,570 The idea is that when this variable becomes true, then the 84 00:04:33,570 --> 00:04:35,169 user entered a valid number. 85 00:04:36,000 --> 00:04:40,870 So the loop test while not done, not done will be true since we set 86 00:04:40,870 --> 00:04:42,560 it to false, so we enter the loop. 87 00:04:43,210 --> 00:04:46,019 In the body of the loop, we prompt the user for a number and read it. 88 00:04:46,840 --> 00:04:50,170 If the number is not valid, we display an error message and loop 89 00:04:50,170 --> 00:04:52,310 again since done is still false. 90 00:04:53,150 --> 00:04:58,020 If the number is valid, we display thanks and set the done flag to true. 91 00:04:58,500 --> 00:05:01,140 Now the loop condition will fail and we exit the loop. 92 00:05:01,910 --> 00:05:05,099 There are many ways to write logic and to achieve the same outcome. 93 00:05:05,529 --> 00:05:07,750 Always remind yourself of the kiss principle. 94 00:05:08,040 --> 00:05:11,290 Keep it simple stupid or keep it short and simple or keep 95 00:05:11,290 --> 00:05:12,610 it simple and straightforward. 96 00:05:12,810 --> 00:05:13,960 However, you want to think of it. 97 00:05:14,410 --> 00:05:16,080 Basically, don't be too clever. 98 00:05:16,270 --> 00:05:18,240 Keep your code as simple as possible. 99 00:05:18,880 --> 00:05:22,169 Let's head over to the IDE and see some other examples of the while loop. 100 00:05:23,359 --> 00:05:27,110 I'm in the section 9 workspace in the while loop project. 101 00:05:28,470 --> 00:05:32,440 What I want to do is write a couple of while loops some that count down, 102 00:05:32,440 --> 00:05:35,460 one that counts up and a couple of more, more complicated ones. 103 00:05:35,950 --> 00:05:39,129 In this case, I've already written one that counts down. 104 00:05:39,410 --> 00:05:42,849 So the idea is we ask the user to end positive integer to 105 00:05:42,859 --> 00:05:45,990 start the countdown and then we start the countdown and we 106 00:05:45,990 --> 00:05:47,420 blast off when we get to one. 107 00:05:47,690 --> 00:05:51,840 So if they type in 5, we'll say 5, 4, 3, 2, 1 blast off. 108 00:05:52,110 --> 00:05:52,630 Okay. 109 00:05:52,660 --> 00:05:55,050 So let's walk through this code so you can understand it. 110 00:05:55,610 --> 00:05:59,540 First thing we're doing is we're defining that num right up here. 111 00:05:59,540 --> 00:06:01,729 That's where the user is going to enter the number into. 112 00:06:02,160 --> 00:06:05,627 And let's say they entered a 3, so they're going to enter the 3, and 113 00:06:05,627 --> 00:06:07,169 we're going to store it in num. 114 00:06:07,490 --> 00:06:11,790 So I've got num over here, and its value is 3 right now. 115 00:06:13,009 --> 00:06:18,020 Now we check this condition is 3 greater than 0. 116 00:06:18,330 --> 00:06:19,540 Well, that's true. 117 00:06:19,770 --> 00:06:24,180 So we execute the output statement, which says just display num, 118 00:06:24,230 --> 00:06:25,390 and I'll do that over here. 119 00:06:25,390 --> 00:06:28,860 So I'm doing the display of 3, and then i decrement num. 120 00:06:29,420 --> 00:06:31,600 Remember, when it's all alone on a line like this, the 121 00:06:31,600 --> 00:06:34,720 pre-decrement in a post-decrement means exactly the same thing. 122 00:06:35,090 --> 00:06:37,390 So I just change num from a 3 to a 2. 123 00:06:38,800 --> 00:06:44,559 I go back up and test the condition again is 2 greater than 0, it is. 124 00:06:44,970 --> 00:06:46,899 I output the num which is 2. 125 00:06:48,169 --> 00:06:49,899 And then i decrement it to 1. 126 00:06:51,080 --> 00:06:54,259 I'll check the condition again is 1 greater than 0, it is. 127 00:06:54,580 --> 00:06:56,110 So I'll display the 1. 128 00:06:56,429 --> 00:06:58,370 Then I decrement this to a 0. 129 00:06:58,480 --> 00:07:00,410 And I check the condition one final time. 130 00:07:00,410 --> 00:07:03,780 In this case, 0 greater than 0 is not true. 131 00:07:04,389 --> 00:07:09,680 So I stop, and I end up executing that line right after the loop blast off. 132 00:07:09,920 --> 00:07:12,869 So what we end up with is 3, 2, 1 blast off. 133 00:07:13,170 --> 00:07:13,520 Okay. 134 00:07:13,560 --> 00:07:16,030 So that's a good way to walk through it. 135 00:07:16,180 --> 00:07:18,100 And what we'll do is we'll run this. 136 00:07:19,270 --> 00:07:20,460 And let's do it with the 10. 137 00:07:22,000 --> 00:07:25,605 And there you can see the code going 10, 9, 8, 7, 6, 138 00:07:25,605 --> 00:07:26,549 5, 4, 3, 2, 1 blast off. 139 00:07:28,180 --> 00:07:32,419 Okay so now let's write a while loop that counts up. 140 00:07:32,920 --> 00:07:37,210 So I'll comment this code out right here, and I'll paste some 141 00:07:37,210 --> 00:07:39,809 code that I've already written to save a little time, and 142 00:07:39,809 --> 00:07:41,120 let's walk through this code. 143 00:07:42,099 --> 00:07:48,479 In this case, notice that I'm defining again declaring and defining this 144 00:07:49,359 --> 00:07:51,489 same variable num to be an integer. 145 00:07:52,110 --> 00:07:55,830 I ask the user to enter a positive into integer to count up 2. 146 00:07:56,100 --> 00:07:59,619 So in this case, I want to count from 1, 2., whatever they typed in. 147 00:08:00,610 --> 00:08:01,490 And I'm reading then. 148 00:08:01,650 --> 00:08:03,539 So again suppose they typed in a 3. 149 00:08:04,840 --> 00:08:08,890 So now I've got num and its value here is 3. 150 00:08:10,110 --> 00:08:13,510 I can't do what i did up here with just a single variable because 151 00:08:13,540 --> 00:08:14,676 I don't want to change num. 152 00:08:14,676 --> 00:08:17,490 Num is my target as to where I'm going to stop. 153 00:08:17,910 --> 00:08:20,880 So i need another counting variable or a looping variable. 154 00:08:20,880 --> 00:08:22,800 In this case, I want to use I. 155 00:08:23,170 --> 00:08:24,670 So I've got I over here. 156 00:08:25,930 --> 00:08:28,150 And I'm initializing i to 1 because that's where i 157 00:08:28,150 --> 00:08:29,659 want to start counting at. 158 00:08:29,900 --> 00:08:31,360 So I becomes 1. 159 00:08:32,139 --> 00:08:32,850 All right. 160 00:08:32,860 --> 00:08:34,250 Now we do the loop. 161 00:08:34,539 --> 00:08:37,780 We test the condition is num greater than or equal to i. 162 00:08:37,940 --> 00:08:42,780 So is 3 greater than or equal to 1, in this case. 163 00:08:43,480 --> 00:08:46,870 This is true so what we do is we display i, and I'll 164 00:08:46,870 --> 00:08:48,069 do the display over here. 165 00:08:48,070 --> 00:08:50,979 We'll display i, which is 1 and then we increment I. 166 00:08:50,979 --> 00:08:53,899 So now I is 2. 167 00:08:54,740 --> 00:09:00,510 We do the loop condition again is num, which is 3 greater 168 00:09:00,510 --> 00:09:03,490 than or equal to 2, it is. 169 00:09:04,790 --> 00:09:08,470 We execute the body of the loop, so we print out I, which is now 2. 170 00:09:10,600 --> 00:09:12,200 Okay, I hope you guys are with me. 171 00:09:12,450 --> 00:09:14,909 I increment I, I now becomes 3. 172 00:09:15,510 --> 00:09:18,477 Then i go back up and I test the loop again 173 00:09:18,477 --> 00:09:20,950 is num which is always going to be 3. 174 00:09:20,950 --> 00:09:23,950 Is num greater than or equal to three, 175 00:09:23,950 --> 00:09:26,350 it is, right. 3 is equal to 3. 176 00:09:26,550 --> 00:09:30,350 I want to output I again. I is 3 177 00:09:30,850 --> 00:09:32,550 and then I increment I. 178 00:09:32,850 --> 00:09:34,550 Now I becomes 4. 179 00:09:35,250 --> 00:09:37,550 We'll test one more time. Is 4 180 00:09:38,950 --> 00:09:40,550 greater than 181 00:09:42,850 --> 00:09:48,520 or -- sorry is 3 greater than or equal to 4. 182 00:09:49,500 --> 00:09:50,640 it is not. 183 00:09:51,030 --> 00:09:52,990 So it fails at this point. 184 00:09:53,170 --> 00:09:55,400 Now we come down here, and whatever statement would 185 00:09:55,410 --> 00:09:56,680 be here, we would execute. 186 00:09:57,340 --> 00:09:57,750 Okay. 187 00:09:58,040 --> 00:10:02,820 So you can see that walking through these examples is pretty easy to do. 188 00:10:02,820 --> 00:10:05,219 It's kind of time consuming, but once you get used to the 189 00:10:05,219 --> 00:10:07,670 logic, you can pretty much do a lot of it in your head. 190 00:10:08,809 --> 00:10:09,109 Okay. 191 00:10:09,110 --> 00:10:10,539 So let's do another one now. 192 00:10:12,299 --> 00:10:15,709 And I'll clear this up, and I'll comment this all out again. 193 00:10:20,950 --> 00:10:23,120 So in this case, what we want to do is, we want to 194 00:10:23,120 --> 00:10:24,689 do some input validation. 195 00:10:25,120 --> 00:10:26,369 And I've written the code again. 196 00:10:26,389 --> 00:10:29,949 Let me paste it in and let me indent all this. 197 00:10:29,949 --> 00:10:31,890 I don't know why it puts it all flush left. 198 00:10:32,770 --> 00:10:33,329 There we go. 199 00:10:34,010 --> 00:10:34,390 Okay. 200 00:10:34,390 --> 00:10:38,360 So we've got an integer here called number that we've declared, 201 00:10:38,389 --> 00:10:40,000 and we've initialized it to 0. 202 00:10:40,460 --> 00:10:43,500 And we're asking the user to enter an integer less than 100. 203 00:10:44,219 --> 00:10:47,529 And we're reading that into that variable number, right. 204 00:10:47,740 --> 00:10:51,520 Now we want to loop, remember, we want an integer less than a 100. 205 00:10:51,970 --> 00:10:56,509 So we want to loop while that number they entered is not less than a 100. 206 00:10:56,510 --> 00:10:59,990 So we could say not less than a 100 or greater than or equal to 100. 207 00:11:00,020 --> 00:11:02,360 Either case, will work here so. 208 00:11:02,360 --> 00:11:04,580 In other words, if the number they entered is greater than or 209 00:11:04,580 --> 00:11:06,810 equal to 100, it's not valid. 210 00:11:07,600 --> 00:11:11,250 So in that case, we'll just ask them to enter an integer less than 100. 211 00:11:11,250 --> 00:11:14,380 Again, we're prompting them to enter it, and we're reading it. 212 00:11:14,900 --> 00:11:17,000 And then we're just checking that while statement 213 00:11:17,010 --> 00:11:18,129 over and over and over. 214 00:11:18,460 --> 00:11:22,060 When this condition fails, that's when we have a valid number. 215 00:11:22,719 --> 00:11:23,109 Okay. 216 00:11:23,289 --> 00:11:26,130 So let's run this, and we'll print out thanks when we have 217 00:11:26,130 --> 00:11:27,160 a valid number at the end. 218 00:11:27,390 --> 00:11:29,490 So let's run this you see what it looks like. 219 00:11:30,280 --> 00:11:32,540 Let's say I want to integer less than 100. 220 00:11:32,580 --> 00:11:37,230 Let's do it correctly, we'll type in 50, and we get a thanks. 221 00:11:37,790 --> 00:11:42,213 But if we run it and type in a number that's not less than 100, let's 222 00:11:42,359 --> 00:11:47,390 say we typed in 120, it prompts me again I'm in the loop now. 223 00:11:47,780 --> 00:11:55,099 So let's say 115, 1000, it'll just keep going forever until 224 00:11:55,330 --> 00:11:56,960 I type in a valid number. 225 00:11:57,219 --> 00:12:00,500 And basically, this is the basics of input validation. 226 00:12:00,760 --> 00:12:03,740 So 99, that's valid thanks. 227 00:12:04,080 --> 00:12:04,880 Okay. 228 00:12:04,910 --> 00:12:05,830 So there you go. 229 00:12:05,830 --> 00:12:08,450 You can walk through this the same way that I walked through the other one. 230 00:12:09,000 --> 00:12:12,149 Notice that you can write this condition in here a lot of different 231 00:12:12,150 --> 00:12:16,160 ways, right because greater than or equal to 100 or not less than 100. 232 00:12:16,530 --> 00:12:18,920 Okay, so there's different ways that you can write that condition. 233 00:12:19,200 --> 00:12:21,140 And you'll see it all kinds of ways out there. 234 00:12:21,430 --> 00:12:26,280 All right, so the last example I want to do is one using a flag -- a Boolean flag 235 00:12:26,320 --> 00:12:31,220 to do input validation so let me comment this out and I'll copy that 236 00:12:31,220 --> 00:12:35,609 code in real quick okay so there's the code now we're going to walk 237 00:12:35,609 --> 00:12:39,460 through this real slow and in detail so you can see what's going on this 238 00:12:39,460 --> 00:12:42,860 is a common use case and this is the kind of code you see out there a lot. 239 00:12:43,150 --> 00:12:46,439 So in this case we've got a flag called done. 240 00:12:46,900 --> 00:12:48,719 And we're initializing it to false. 241 00:12:49,780 --> 00:12:54,150 False means that we're not done right we still need to get more input from 242 00:12:54,150 --> 00:12:57,560 the user for whatever reason either we haven't started yet or they've entered 243 00:12:57,560 --> 00:12:59,830 something illegal or out of range. 244 00:13:00,800 --> 00:13:04,080 And I've got this variable right here that I've declared here called 245 00:13:04,090 --> 00:13:06,510 number that's the integer that's what we're going to read into. 246 00:13:06,990 --> 00:13:10,809 Now it's important that we set that flag to false to begin 247 00:13:11,070 --> 00:13:14,240 because that's how we're getting into this loop the first time. 248 00:13:15,310 --> 00:13:18,800 So don't just leave that to true or just don't leave it to nothing 249 00:13:18,800 --> 00:13:20,490 and take a chance that it is false. 250 00:13:21,160 --> 00:13:23,819 Explicitly set it to false because that's going to get us 251 00:13:23,870 --> 00:13:25,300 in that loop the first time. 252 00:13:25,610 --> 00:13:28,899 So the first time we're going to say while not done well it 253 00:13:28,900 --> 00:13:30,290 isn't done right it's false. 254 00:13:30,670 --> 00:13:35,500 So we're in here and we ask the user to enter an integer between 1 and 5. 255 00:13:36,150 --> 00:13:38,900 And they type the number right there. 256 00:13:38,900 --> 00:13:40,710 We're going to store it in the variable number. 257 00:13:41,630 --> 00:13:42,760 Now we do a check. 258 00:13:43,299 --> 00:13:45,649 If the number is less than or equal to 1. 259 00:13:45,650 --> 00:13:47,210 Remember, we want numbers. 260 00:13:47,350 --> 00:13:49,630 There's the 4, here's the 5. 261 00:13:49,890 --> 00:13:51,950 So we want numbers between there, right. 262 00:13:52,350 --> 00:13:55,770 So we want 2, 3 or 4. 263 00:13:55,770 --> 00:13:57,120 1 and 5 are not valid in this case. 264 00:13:57,420 --> 00:14:00,309 So if the number is less than or equal to 1, in other words the 265 00:14:00,379 --> 00:14:05,360 number is over here somewhere or the number is greater than or equal 266 00:14:05,360 --> 00:14:10,000 to 5, so the number is over here somewhere, then it's out of range. 267 00:14:10,460 --> 00:14:13,090 So we simply display out of range try again. 268 00:14:13,550 --> 00:14:16,140 Notice how we didn't change the done flag. 269 00:14:16,320 --> 00:14:18,650 The done is still false, right. 270 00:14:18,889 --> 00:14:22,180 So at this point, we just executed that statement. 271 00:14:22,440 --> 00:14:28,219 We don't do the else part and we loop right back up to here, right. 272 00:14:28,230 --> 00:14:29,310 Done is still false. 273 00:14:29,320 --> 00:14:32,319 So we're going to go right back into the loop and output again 274 00:14:32,370 --> 00:14:36,190 enter an integer between 1 and 5, grab the integer and so forth. 275 00:14:37,070 --> 00:14:41,010 Now when you do type an integer that's in range, suppose we typed in a 3. 276 00:14:42,369 --> 00:14:45,570 Then this if statement fails here and we go to the else. 277 00:14:46,990 --> 00:14:49,060 And all we're doing here is saying, thanks. 278 00:14:49,849 --> 00:14:52,420 And then we set that flag to true. 279 00:14:52,480 --> 00:14:55,120 This is like super, super important here. 280 00:14:55,120 --> 00:14:57,300 Don't forget to do that. 281 00:14:57,300 --> 00:15:03,069 That means we are done so when we loop back up again here, that condition 282 00:15:03,070 --> 00:15:05,250 fails, and we're out of the loop. 283 00:15:06,390 --> 00:15:06,940 Okay. 284 00:15:06,990 --> 00:15:10,050 So hopefully, you can walk through this and just take it a step at 285 00:15:10,050 --> 00:15:11,429 a time and really understand it. 286 00:15:11,940 --> 00:15:15,850 The challenge exercise at the end of this section is a pretty 287 00:15:15,920 --> 00:15:17,460 tricky and hard challenge. 288 00:15:17,799 --> 00:15:22,090 But it uses this kind of logic to ensure that you've got input 289 00:15:22,090 --> 00:15:25,110 validation and you're going to prompt the user every time that 290 00:15:25,110 --> 00:15:26,860 they are not in range, right. 291 00:15:26,860 --> 00:15:30,550 So let me clear this and let's run this example. 292 00:15:35,020 --> 00:15:37,586 So we're going to enter an integer between 1 and 5. 293 00:15:37,690 --> 00:15:38,710 Let's say we type 1. 294 00:15:38,710 --> 00:15:41,180 1 is not valid. 295 00:15:41,180 --> 00:15:42,960 We want it to be between 1 and 5. 296 00:15:42,970 --> 00:15:44,345 Again, 2, 3 or 4. 297 00:15:44,510 --> 00:15:45,530 So that's no good. 298 00:15:45,840 --> 00:15:46,960 5 is no good. 299 00:15:47,759 --> 00:15:49,700 70 is no good, right. 300 00:15:49,730 --> 00:15:51,389 Negative 12 is no good. 301 00:15:51,990 --> 00:15:53,689 So let's give it something good. 302 00:15:53,699 --> 00:15:54,580 Let's give it a 3. 303 00:15:54,590 --> 00:15:57,000 Something that's right in the middle, thanks. 304 00:15:58,450 --> 00:16:04,300 So you can see that using this kind of logic is better than using 305 00:16:04,309 --> 00:16:07,520 this sort of logic, where you've got a statement repeated here and 306 00:16:07,520 --> 00:16:10,789 then the same statement over here, and you've got duplicated code. 307 00:16:11,190 --> 00:16:12,739 We don't have that here. 308 00:16:13,270 --> 00:16:15,370 And the reason we don't have that here is because we're 309 00:16:15,370 --> 00:16:19,310 using that flag to really decide whether we're in the loop or not. 310 00:16:20,190 --> 00:16:23,550 Now in the next video, we'll talk about a do while loop, 311 00:16:23,580 --> 00:16:26,289 where we're doing the condition at the end and that makes 312 00:16:26,290 --> 00:16:27,940 this kind of code even nicer. 313 00:16:28,440 --> 00:16:30,609 Okay, so that's the end of this video. 314 00:16:31,450 --> 00:16:34,740 Please try these examples yourself, come up with your own versions 315 00:16:34,750 --> 00:16:37,530 of them, and walk through them so that you really understand 316 00:16:37,530 --> 00:16:39,060 the logic, and what's going on.