02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Break In Action: Prime Numbers

Lets practice our new concept of break and use it to create a nested loop that searches for prime numbers.

Video Transcript

00:01Let's see break in action, and actually what we're going to do is we're going to try, well hopefully we'll succeed, to find prime numbers.
00:09Let's try to find the prime numbers between, let's say, 2 to 40, because we know that 1 and 2 are prime numbers already.
00:19So we probably could skip 1 and we could skip 2, and we could start looking for our prime numbers from 3 and onwards.
00:27So let's start by checking for var i, which is an integer, and we're going to start from 3.
00:33And as long as i is smaller than 40, and we're going to make i bigger each time.
00:40So far, so good. Now, how do we check to see if a value is a prime number or not?
00:47Now the rules of a prime number is if that number does not divide by 2, right?
00:55It does not divide by any value that between 2 to its value, right?
01:02It could divide by 1 because everything divides by 1, but it can't divide by anything else besides itself and 1 in between, right?
01:10So let's take that step and create another for loop.
01:15In that for loop, we're going to run through all the values, and I'm going to set i as an integer.
01:21Oh, sorry, we already did i, so we're going to set j as an integer, and we're going to set its value to be 2, all right?
01:30Because the number could divide by 2, and if it divides by 2, then it's not a prime number anymore.
01:36And as long as our j value is smaller than i, then we want to continue in checking our j.
01:43And we want to make sure the j grows each time, and we open and close a curly bracket.
01:50All right, so now we want to check to see if our j value, if our i value divides by j.
01:57Now the easiest way to do that is to actually just do a mildew.
02:01If I check if i mildew j equals 0.
02:07Now if it equals 0, what does that mean?
02:09That means those numbers divide by, right?
02:11So it means that i is divisible by j if there's no remainder.
02:17If there is no remainder, we could now use our break, because we already know that our i could never be equal to j.
02:27Well, we know that our i could never be a prime because it divides by j, right?
02:33So let's break out, but before we break out, maybe we want to set some sort of variable to let us know that it is a prime, all right?
02:40So I'm going to set here another variable.
02:42I'm going to call it isPrime.
02:45And we're going to set it as a Boolean.
02:46I'm going to set the default to be false, which really doesn't matter because we don't need to even have to do this initially
02:55because we're going to have to set that up each time, right?
02:58Each time we check a new i, we want to set it initially to be isPrime equals false.
03:04So as soon as we start with 3, we're setting it to be false.
03:07It's not a prime number unless...
03:11Well, actually, let's do it the other way around.
03:14Let's set it to be true.
03:15So we're assuming our number is a prime number unless it's not, right?
03:19And this matches the unless it's not scenario.
03:23So we could say that isPrime is not true anymore.
03:28Not only it's not true, we want to break out of that loop.
03:32Now, what does that mean, breaking out of that loop?
03:34We can't break out of an if statement.
03:35So basically, our break doesn't interact with our if, right?
03:39Our break is interacting with its closest thing that's breakable.
03:42So an if statement is not breakable, switches are, and for loops are, and loops in general are.
03:49So we're going to break out of that for loop no matter what position we're in.
03:53Even if we're just in the beginning of our for loop, it really doesn't matter
03:57because as soon as it hits a break, it leaves that for loop.
04:01So now we left our for loop, and the last thing that we really have left to do is to check if isPrime.
04:07That means we never found a value that was divisible.
04:12We want to trace out and say that i is a prime number.
04:19Let's go through this one more time before we run it and see if we're right or we have any error.
04:27First of all, we're running through the values 3 through 40, so smaller or equal.
04:35And we're running through that i value.
04:37Initially, we're saying that our i, no matter what value it is, is a prime number.
04:41Unless our for loop finds a match of some sort of division, all right?
04:48So if our prime, if our i does divide by j in this for loop,
04:53and this for loop of j starts from 2 and ends in whatever position i is.
04:57So in the first round, it's from 2 through 3.
05:01And when our i equals 40, then it will be from 2 through 40.
05:06Now, as soon as our if statement hits a correct value, that means if i mold j,
05:13that means if there's no leftover or when the leftover returned a 0,
05:17which means that the two numbers are divisible,
05:22then we're going to set the prime to be false and leave that for loop.
05:25Now, if this never happens, that means we have a prime number.
05:29And then we're going to trace out a prime if our is prime never reset
05:34since we set it up initially in the beginning of our loop.
05:39So let's run it and see how many numbers between, ooh,
05:42and we have a little bit of a problem.
05:44Let's see what is our error.
05:46And we'll see that we're probably missing here something.
05:49Let's see what we've done wrong.
05:52And we could see that this is not really valid, right?
05:56I've added here a bracket which was not needed.
05:59And we have the same, nope, we have just that problem there.
06:02Let's run it one more time and see if we could run it.
06:06And by the way, we got here an error that is really not descriptive,
06:09right? It's telling us an invalid XML name.
06:12And if you know XML, XML is basically a syntax word in Flashy.
06:16You could add XML.
06:18And Flash thinks that we were trying to type in XML,
06:21but we typed an invalid XML.
06:24But that's not our scenario.
06:26So even though our error message was not descriptive,
06:29it was strong enough to let us know that our error is more or less around line 8.
06:34Now, it could have been in line 7 and it could have been in line 9,
06:37but it gave us enough information to be able to find that our issue was right here.
06:45Now, if we run our application now that we fixed that error,
06:48we should get all of the prime numbers between 3,
06:52which 3 is a prime, 5, 7, and so on,
06:55all the way through 40, which 37 is the last prime number in these areas.
07:01So we've seen how we could actually leverage break
07:04and how powerful it is whenever we're really digging into some scenarios.
07:08So maybe in our initial sample,
07:10it didn't make a lot of sense that break,
07:12but here's a very useful scenario where we're trying to find prime numbers
07:16and how we use break to cut down the amount of code that's actually running,
07:21because our application would run either ways, right?
07:24Our application would run without the break as well,
07:26but our break made sure that we're not going to run through this loop more than we need to.
07:31As soon as we found this value,
07:33we really don't need to continue the loop anymore.
07:35And because we don't need to, we're just going to leave that loop, all right?
07:39So even though in theory we could have run our application without breaking out,
07:43that would be bad coding because we're going to have loops that are running around without any need.
07:48And we really want to cut loose our loops as soon as we don't need them anymore.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!