Loops To Find Something
Loops are not dumb operations used only to repeat an action, they are much more then that. In this video we will take a first stab at looking at how powerful loops are for operations that are not possible without loops.
Video Transcript
00:00We've seen how to work with loops as a means to repeat an action and then we've seen how to modify that action
00:07So it's not exactly one-on-one
00:09but we said that there's a few different types of
00:12Things we could use loops for and actually we're focusing now only on two things, but there are many more
00:19So the second thing that we said we could do we could use loops are for our for mathematical operations or for
00:26organizational or
00:28finding things to do that we're trying to we're going to try and find some sort of a hybrid between finding things and
00:36mathematics
00:37To do that. I want to create actually a function and that function
00:41I'm gonna start off as a private function and again for now
00:45you could ignore that private attribute and actually if you want just add that function inside of your main or
00:50Just repeat after me and later on as we delve into the world of object-oriented programming and we see
00:57We'll see deeper what that means. But at this point just as a reminder
01:02When we define a function inside of our main classes
01:06We've done so far the scope of that function is only within the brackets, right?
01:11But now that we're creating our function outside of the bracket area of our constructor
01:15We're actually creating it in the area of the class
01:19Now without even knowing what that class is even though we know that class is basically our whole application and the scenario
01:25It's our heart. It's our the what we're initiating for now. We could say it's the only object on the stage a
01:32Method that we create a function method that we create inside of the area inside of the our class would have a scope of
01:41Everything inside of that class
01:43that's if we try to call that function from our main it would have access because our main is living inside of that scope and
01:49It has access to other siblings and that's all we really need to understand at this point
01:55so let's create a new function which we're going to call max and it's going to retrieve an array and
02:01Let's create some random numbers. So let's say we have your 30 minus
02:06240
02:093
02:110.5
02:15100,000 and
02:18201 and
02:20Let's say minus
02:221,300 and 21 as well. All right, so we have your a nice array and let's put your zero as well
02:27So we have a nice range of numbers both positive negative both floating and real real and integers
02:34And let's send that value to our max and what I would want to do is I want to trace out
02:40the value that max returns so I'm going to send to max a
02:44Numbers and the value that's coming back. We're going to trace out and see what outputs
02:50Next step that I want to do is actually go into my max
02:53function and actually implement our
02:57Logic to find out that max number. So to start I'm going to create a variable which we're going to call it the max
03:03value and it's a number and
03:09I'm going to set that value initially to the biggest possible number that our computer recognizes for the number type
03:15to do that, I'm going to use a
03:18Constant and a constant is the number that flash ready has in memory and we're going to we're going to access number and
03:25then we're going to type dot and we're going to select the
03:28Min value the reason why we're selecting mid value is because we want to start from the lowest possible number that our type number could offer
03:37And after we found that lowest type
03:40value we could now loop through all of the array numbers all of the array values and
03:46Then find what is the highest number?
03:49Alright, so let's see how we do that. First of all, let's create another variable and we're going to call that variable length
03:55Which is going to be a type of integer and we're going to get from our a which is an array
04:03We're going to ask for its length. So now we know the length of our array
04:08Because arrays have a property that returns a value
04:12Of how long it is now the next step is actually we're going to create that for loop
04:16So I'm going to start with a 4 and the first parameter as we said we're going to instantiate a new variable of type of integer
04:24Set it to 0 and
04:26The second parameter is our condition as long as I is smaller than the length of the values that are coming from that array
04:33We're going to every time add one to that I and the next step after we open and close a bracket is actually to
04:40test our value
04:42So how do we test that value? First of all we could ask if
04:46max value is
04:49What if max value is smaller than the current value in a so I could go to my a which is my array and
04:56In position I which means in the beginning would be in position zero
05:00Which would be 30 and then position one which would be minus 200 and so on and so on until it reaches the end of
05:06the array, so I'm asking if my max value is
05:09smaller or
05:12If our max value is smaller than the number that's inside there
05:16What does that mean if our max value is smaller than the number inside here?
05:20That means this is bigger than our value if this is
05:24Bigger than our max value then we should set our max value to equal that value
05:29Let's see what's going on here. So first off
05:38we're starting from the most negative minus number possible as
05:43We're looping through the array the first time we encounter 30 what happens where basically we're in position zero in our array
05:51Which is equals 30?
05:53It checks to see if minus the really big number is smaller than 30 and definitely is if that's the case in max value
05:59No equals 30
06:01Right. So now max value equals 30 and we finish up that loop and and that I jumps one value
06:09So now I equals 1
06:11When I equals 1 then we're asked we're still smaller than the length
06:14So we're back into our if statement and in our if statement
06:17We're asking is the value in position 1 which is minus 200 is the value minus 200 larger than 30?
06:24No, it's not larger thus
06:25we're gonna skip that if statement and we're back to the beginning and then we move on to 2 and
06:30Then 40 is bigger than 30. So it retrieves that value 3 isn't 0.5 isn't but this value is
06:38Right. So let's return the last item that's left is actually to return the value of max value
06:46Now that we're returning the value of max value, let's see what happens here. So when we return
06:51First of all, we see for in action when it comes to sorting and when it comes to mathematical operations
06:57What we're doing here is we're looking for the biggest number as we found the biggest number
07:01We're sending it back to our trace as our trace will output should output the highest number which is
07:07a hundred thousand two hundred and one so if we run our application one time
07:14We should get the highest number and we indeed do we get a hundred thousand two hundred and one
07:19Let's try this again and go through it a little bit quicker this time around. Let's try to trace out the minimum number
07:25So let's let's create a new function call it min
07:28And I could actually duplicate the max function because max and min are very similar
07:33And if I go down to my min now, I'm going to call it min
07:37What do we want to change? First of all, we don't want to get the min of value
07:40We want to start from the biggest possible value and then get smaller than that again
07:44We want to retrieve the length of our array and then in our for loop
07:49And then then for our for loop, we're going to add those three parameters
07:52First of all, we're initiating I and notice again them initiating I again and that's fine because we're in a new scope that I
07:59Scope belong to the previous function and this I scope belongs to the min function
08:04And although they have the same name, they're completely different variables
08:08All right, so we set it to zero
08:10We're checking if our eye is smaller than our length
08:13That means our loop is going to continue as long as there's something inside of our array and if they're when it finishes up
08:19Then it will add one to our current eye and then check again for our index. All right, so
08:26What do we have to change inside of our for loop? First of all our max value is
08:31Is price should be min value now?
08:35We could change that to min value. Well, that's just
08:38Textual and min value our min value. First of all, is this the right question? We want to ask
08:43No, we want to ask the exact opposite
08:45We want to ask if our minimum value is
08:48Larger than the current value if it is larger than the current value then we'd want to replace our current value
08:55Well, we want to replace the value inside min value to be the current value
09:00And again, then we would return that min value. Thus if we run our application one more time
09:05It should then retrieve the lowest number which is minus fifty thousand three hundred and twenty one and it does exactly that
