1 00:00:07,910 --> 00:00:12,330 Welcome back. Break and continue are two special kinds of 2 00:00:12,330 --> 00:00:16,200 statements that can be used within while or for loops. 3 00:00:16,200 --> 00:00:20,250 A break statement breaks out of whatever loop contains it. 4 00:00:20,250 --> 00:00:25,620 So here, if we're within a loop and we encounter a break statement, 5 00:00:25,620 --> 00:00:29,275 then Python is going to break out of the loop immediately. 6 00:00:29,275 --> 00:00:32,630 So, this break statement is going to say that the program should jump 7 00:00:32,630 --> 00:00:35,900 from here to here and it's going to 8 00:00:35,900 --> 00:00:39,320 skip the rest of whatever is in the body of the 9 00:00:39,320 --> 00:00:43,580 for loop and it's not even going to check the condition again. 10 00:00:43,580 --> 00:00:46,620 A continue statement is similar. 11 00:00:46,620 --> 00:00:48,800 A continue statement like 12 00:00:48,800 --> 00:00:52,190 a break statement does skip whatever is in the rest of the loop. 13 00:00:52,190 --> 00:00:54,370 So, it's not going to run this code, 14 00:00:54,370 --> 00:00:59,840 but unlike a break statement which takes us to the bottom of the loop, 15 00:00:59,840 --> 00:01:04,550 a continue statement instead says to continue at the top 16 00:01:04,550 --> 00:01:09,395 of the loop and so it's going to check this condition once more. 17 00:01:09,395 --> 00:01:14,640 So, let's see what break and continue statements do in code. 18 00:01:14,780 --> 00:01:17,500 So, here we have a while loop, 19 00:01:17,500 --> 00:01:22,790 this condition is true meaning it's always going to be, well, true. 20 00:01:22,790 --> 00:01:25,790 So, what that means is that without a break statement, 21 00:01:25,790 --> 00:01:29,840 then this is almost by definition going to be an infinite loop. 22 00:01:29,840 --> 00:01:32,705 But, here in the body of the loop, 23 00:01:32,705 --> 00:01:33,950 we first print out this, 24 00:01:33,950 --> 00:01:38,240 this phrase will always print and then we call break and then we 25 00:01:38,240 --> 00:01:43,010 say print does this phrase print and then here we print out, 26 00:01:43,010 --> 00:01:44,600 we're done with the while loop. 27 00:01:44,600 --> 00:01:50,130 So, I want you to think a little bit about what this code is actually going to print. 28 00:01:50,290 --> 00:01:53,810 So, what I expect to happen is that when we run 29 00:01:53,810 --> 00:01:56,960 this code even though this says while true, 30 00:01:56,960 --> 00:02:00,485 this is only going to print out once because after this prints out, 31 00:02:00,485 --> 00:02:03,065 then we break out of our while loop. 32 00:02:03,065 --> 00:02:06,680 Then, we skip what's here because that comes after 33 00:02:06,680 --> 00:02:10,535 the break and then we print out we're done with the while loop. 34 00:02:10,535 --> 00:02:15,725 So, I expect this and this to print out. 35 00:02:15,725 --> 00:02:19,170 Let's run our code to be sure that's the case. 36 00:02:19,420 --> 00:02:27,420 So, you can see the only things that print out are this statement and this statement. 37 00:02:27,670 --> 00:02:32,760 Now, what would happen if I replace the break with a continue. 38 00:02:34,310 --> 00:02:36,640 So, when I run this code, 39 00:02:36,640 --> 00:02:40,390 what I should expect is that it's going to get stuck in an infinite loop. 40 00:02:40,390 --> 00:02:45,350 The reason is that, we are online to print out this phrase will always print and then 41 00:02:45,350 --> 00:02:50,700 this continue statement jumps to the top of the loop and checks the condition again, 42 00:02:50,700 --> 00:02:55,210 and here this condition again by definition is going to be true, 43 00:02:55,210 --> 00:02:57,530 and so we're going to print this out again and then 44 00:02:57,530 --> 00:03:01,460 continue and so we have an infinite loop and I expect 45 00:03:01,460 --> 00:03:04,910 this phase will always print to read printed out 46 00:03:04,910 --> 00:03:09,230 while a huge number of times before our program actually stops terminating, 47 00:03:09,230 --> 00:03:11,735 and you can see when we look at our code, 48 00:03:11,735 --> 00:03:14,185 but that's exactly what happened. 49 00:03:14,185 --> 00:03:19,590 So, let's look at another example to see how a continue statement works. 50 00:03:19,930 --> 00:03:24,955 So, here we have a slightly more complicated piece of code. 51 00:03:24,955 --> 00:03:27,210 We have a number x, 52 00:03:27,210 --> 00:03:28,635 which we set to zero, 53 00:03:28,635 --> 00:03:32,380 and then we say while x is less than 10, 54 00:03:32,380 --> 00:03:34,895 and as long as x is less than 10, 55 00:03:34,895 --> 00:03:37,745 we print out we are incrementing x. 56 00:03:37,745 --> 00:03:39,830 Now, what we do is we say, 57 00:03:39,830 --> 00:03:45,080 if x is even so in other words if x modulo two is zero, 58 00:03:45,080 --> 00:03:47,795 then we add three to the value of x. 59 00:03:47,795 --> 00:03:51,020 So, x would jump from zero to three, 60 00:03:51,020 --> 00:03:52,834 and then we say continue, 61 00:03:52,834 --> 00:03:57,690 which takes us back to the top of this for loop Then, 62 00:03:57,690 --> 00:04:00,510 we say, if x modulo three is zero, 63 00:04:00,510 --> 00:04:03,215 in other words, if x is divisible by three, 64 00:04:03,215 --> 00:04:08,460 then we assign x to be x's value plus five and almost regardless, 65 00:04:08,460 --> 00:04:10,300 then we add one to x. 66 00:04:10,300 --> 00:04:11,800 By the time we're done, 67 00:04:11,800 --> 00:04:14,995 we print out done and we print out the value of x. 68 00:04:14,995 --> 00:04:19,370 Now, let's run this code and CodeLens to see what happens. 69 00:04:20,510 --> 00:04:25,755 So, again we start x is zero and we say while x is less than 10, 70 00:04:25,755 --> 00:04:27,630 print out we're incrementing x. 71 00:04:27,630 --> 00:04:29,745 Here x is divisible by two, 72 00:04:29,745 --> 00:04:32,430 so x is going to jump from zero to three. 73 00:04:32,430 --> 00:04:35,300 Now, again, when we hit this continue statement, 74 00:04:35,300 --> 00:04:39,260 we're going to go to the top of this while loop. 75 00:04:39,630 --> 00:04:42,340 So, we jump back up to the top, 76 00:04:42,340 --> 00:04:45,560 we ask is three less than 10? Yes it is. 77 00:04:45,560 --> 00:04:48,530 In this case three is not an even number, 78 00:04:48,530 --> 00:04:50,710 so we check is it divisible by three, 79 00:04:50,710 --> 00:04:56,200 which it is and so we add five to x and then we add one more on the x, 80 00:04:56,200 --> 00:04:58,415 and so x ends up with the value nine, 81 00:04:58,415 --> 00:05:00,020 nine is less than 10, 82 00:05:00,020 --> 00:05:02,450 so we print out we're incrementing x, 83 00:05:02,450 --> 00:05:04,100 nine is not even, 84 00:05:04,100 --> 00:05:05,980 but it is divisible by three, 85 00:05:05,980 --> 00:05:12,860 so x gets the value 14 and then 15 and then now when we check our condition, 86 00:05:12,860 --> 00:05:14,615 15 is not less than 10, 87 00:05:14,615 --> 00:05:20,045 so we say we're done with our loop and x has the final value of 15. 88 00:05:20,045 --> 00:05:24,215 So again, here we use the continue statement 89 00:05:24,215 --> 00:05:28,370 to ensure that we aren't going to run what was in the rest of this loop, 90 00:05:28,370 --> 00:05:31,715 we instead jumped to the top of the while loop. 91 00:05:31,715 --> 00:05:35,250 That's all for now, until next time.