Pay everyone Mark Price you're at death's slopes.
And today we're going to learn how to work with loops and you've learned a little bit about a race.
You're going to see how loops and arrays go hand-in-hand together go ahead and create a new playground
file new playground and let's call this loopy loops because it sounds so much fun.
Am I saying it right here.
Perfect.
OK.
So in order to appreciate and understand the let's talk about some of the problems that we run into
when we don't use loops.
OK.
And we've kind of used this before this scenario in working with arrays.
So let's do the same thing now.
So we've got employees or rather we've got employees who have salaries bar employee one salary.
Right.
So we've got four employees employees each with their own salary.
Forty five thousand dollars for employee 1 bar employee 2 salary equals one hundred thousand dollars.
Our employee three salary equals fifty four thousand dollars and bar employee for salary equals $20000.
OK so we've got four different employees here.
All right now let's say that we had a really good year at the company.
OK.
And we want to give every employee a raise.
So we need to do that.
You know we were a big company we need to make sure that we can automate some things here.
So we're writing an app that allows us to give instant salary increases to employees.
So how do we do that.
Let's say also that we need to give them a certain percentage of a salary raise.
So listen to that as well too.
So say employee one salary
employee one salary equals employee one salary plus employee one salary is 0.1 zero.
OK so we're doing some basic high school math here.
We're taking the salary of the first employee multiplying it by decimal point 10 which is 10 percent.
So we're going to find out what 10 percent of their existing salary is and then we're going to add it
back onto the salary here and store it into employee one salary that's all that's happening.
We're giving this person a 10 percent increase in their salary.
And they're very happy about that.
Don't want any bugs in this app.
That's for sure.
Especially if it's for your own salary.
All right so employee to salary his employee salary plus employees DOLLERY times zero point one zero.
You're probably thinking to yourself.
All right I get the point Mark.
You don't have to keep writing all this code as is a drag.
Well I mean write it down anyway because you need to really appreciate the pain of doing this kind of
stuff because if you're doing this kind of stuff in your code you're doing it wrong.
There's definitely a better way to code.
This is called code smell when you're like something it doesn't seem right about this.
Well we're also breaking the dry principle here too.
Don't repeat yourself.
All right.
So you've just given every employee here a 10 percent raise right.
That's great.
Now the problem comes in especially when we have like 50 employees or a hundred employees or are you
going to write an app that stores all this.
Also what if we hire new employees or fire employees.
How do we know how many employees are.
Every time we get a new employee We're going to have to change our code to add the employees in here.
This is not a great way to do things.
Not at all.
And also it's not a good idea to keep these off separately like this as well too.
So this is bad.
Ok this is the bad way.
Bad way.
All right let's clean some things up.
You know it's but these employees salaries and rates of our salaries equals and then we can just do
some copying and pasting here so paste you know this is an array.
I've talked about this before.
Command c command B and C is copying and pasting here.
So there's our array of salary's so Elissa's is better.
This is mo better putting it in an array.
Ok but how would we go through these and add 10 percent each person's salary.
Well it would look something like this would be salary's zero.
Equals insane salaries zero.
Grabbing the first salary out of here plus salaries zero times zero point one zero.
And then same thing as salaries one equals salaries one plus salaries one times 0.0 etc. etc. for the
other two.
OK.
So is this any better than this.
Not really.
We're still writing lots of code.
So this is definitely better putting in an array versus four different variables.
But this is no good.
This is a lot of manual work and again we don't know.
You know I'm putting numbers in here but we don't know how many employees are actually in the lists.
You know it's going to come into the app and we wouldn't know unless we count them on and changed our
code which is you don't do that nobody rewrites code and does all this stuff manually based on some
condition in the real world.
So how do we solve this problem.
Well we can solve this problem.
That's making our life so miserable right now by using a loop and let's create our very first loop.
We're going to do a a while loop.
OK.
So we're going to say var X equals zero.
This is going to be an index and then we're going to say repeat while X is less than salary's does count
more in say X plus equals one OK.
So what is going on here.
Well look over here on the right it says it ran this loop four times.
So here's the syntax.
The keyword is.
Repeat.
OK.
Repeat.
And then a opening bracket and closing bracket inside we're incrementing X by 1.
It's a variable of type and.
And then while X is less than salary's that count so every loop in programming will repeat itself until
some condition is met.
OK that's what you need to remember.
It doesn't.
Don't think of the syntax or different types of loops loops will repeat until some condition is met.
And in this case here in the parentheses while X is less then salaries that count let's say how many
items are in salaries.
One two three four.
So while X is less than 4 Well it starts at zero.
So this is what's going to happen this loops going to be run once for sure no matter what.
And X is now equal to 1 x plus equals one.
OK.
And then it says while X is less than Sollars accounts while one is less than Sollars that count.
OK.
It is still less than salary's that counts so it's going to go through again from top to bottom over
and over and over again until X is now equal to 4 as well as some point.
This will increase X to 4 and then this will no longer be true because four is not less than salary's
that count four is not less than four is it.
No 4 is equal to four.
OK.
So there's a difference here.
If I was to say less than or equal to it would run five times.
But we don't want it to run five times.
OK.
Because that would crash our our application for accessing elements out of this array.
So how do we use this loop to solve our problem.
Well let me tell you there Bob we first crash X code.
That's the right thing to do.
Let's open up ex-coach again are you ready.
So here goes.
Salary's X equal salaries X plus salaries X times zero point 1 0.
OK well this is interesting.
What's going on here Mark.
Why are you putting an X inside of an array that doesn't make any sense.
I thought a raise couldn't take strings.
Well it's not a string right.
It's an INT.
And this is also when you access elements out of your array using it.
OK you put in the subscript here.
We want the first element or the second element.
So the same things happening here we have a variable that represents our index.
So what we could also have done is called this index that makes more sense to.
And then what you can do.
You can just pass the index here and say that it's not that though.
And then right here and right here.
So a little more wordy but it makes more sense now.
So we have an index where passing in the index and that's giving us the element.
So it's going to repeat and grow items out of that.
So let's look at this again salary's index Well if it's the first 10 through indexes equal to zero sorgen
to grab 45000.
So we're seeing this slide here is equal to salary's index.
So it's equal to 45000 plus 45000 times point 1 0.
Okay.
So we're going to take that.
The amount here that 10 percent we're going to add on to 45000 and then we're going to store the result
of this operation here now OK.
And we're going to pass it or as a sign it here into our salaries index into the same spot that we wanted
to.
And so this is cool.
So yeah we've got 1 2 3 4 5 lines of code here versus potentially.
Well let's look at this here.
If we want to do the same thing over here we would have 1 2 3 4 lines of code and 5 year like haha.
Mark it's not any less code it's the same amount of code that we wrote before.
Well what happens if your company has a lot of employees so
what has this many employees there's 28 employees.
Now if I wanted to do this down here I'd have like 20 lines of code that's no good.
Plus this whereas this will always stay a 5 even if I had a million employees.
So we're starting to make really good progress here again.
So aloo all the loops going to do is go over and over and over and over again until some condition is
met.
In this case well index is less than salary's account.
Okay.
If I didn't have this line of code here do you know what we what we would have running here we would
have an infinite loop.
This will eventually crash the program.
Luckily the playground smart enough to stop it for us.
But if you did something like that in your app you would get an infinite loop.
Your app would to run out of memory and it would crash and the user would give you a one star review.
This developer does not know how to make apps.
So you always have to have a way to get out of a loop.
And it's by meeting the condition.
And so that's why we're increasing the index by 1 each time.
So eventually we can meet the condition.
So that's how loops and arrays go hand-in-hand.
You have a set of data and you can iterate through the data with a loop.
Does that make sense.
Is anyone confused because if you are what I want to do is posit video you can watch this portion again
or you can go online and do some research on how to loops work in programming and making some more under
the hood.
Computer science aspects and theory about it.
OK.
So this is a repeat while now you're not going to use a whole lot.
I wanted to show you the repeat while loop because it's very wordy and it makes sense when you look
at it the loop that you're probably going to use more so is a for loop.
Let's do that next.
Let's bring it down here.
OK.
So we're in right the exact same code with even less lines of code which is cool.
So for X or let's say I to represent index in zero dot dot less than salary's dot count and then what
we'll do is we'll just copy this one line of code here and paste it here.
So now we're doing the exact same thing.
This should be in the index
we're doing the exact same thing here and three lines of code that we didn't have five lines of code
so it's smaller.
So let's talk about what's happening.
So for meaning given or for each k is a this is called for in loop.
As you can see the 4 in the end.
So for i in 0 dot dot less than Sollars accounts what we're doing here is we're specifying a range.
OK we're saying start at zero and go all the way to salary's that count which is currently 28.
But we need to be minus one because arrays start at zero which is why we use the less than sign.
So Dot Dot less than you know so we don't put a space before this or a space after.
OK.
So between 0 and salary's that counts between 0 and 27 and then we go through and we run this and it's
going to do the exact same thing as this.
OK and if you're confused at all I mean just do we write this up in even simpler terms so for let's
say X in one dot dot dot five that's just print index.
So we're going to say is equal to the x
K 5 times.
Here as you can see it printed out five times for X in one through five.
So what has happened here.
It's inclusive.
OK.
So it include the one and it included the five and everything in between.
So this is a range you can call it a sequence you can color range.
So it's a sequence of numbers or range and that's the condition.
So for for every You're going to run this loop for every item in this range between 1 and 5 or including
one in five inclusive.
Same thing here.
OK.
Between 0 and salaries that count are less than Zeller's account so that means 27.
OK.
So this one is inclusive This one's exclusive of the number that you put here.
OK.
So in this case here is do the same thing over here again for z in one thought less than five percent.
And next the.
And then we'll go ahead and put Z in here.
OK.
So in this case it only ran one two three four.
So this was Xclusive it did not include the five because we had the less than sign there.
OK.
And what's happening is every time he goes through this loop it passes that value of this range here.
The current iteration of that range it passes it here into this constant this number cannot be changed.
It passes it in here so you can use it for whatever you want.
In our case here we're using it each time to access the element in the array.
So in this case it's going to run this code 28 times between 0 and 27.
It's going to pass that number into every single time 0 1 2 3 4 or 5.
And then you can use those numbers on your array elements which is pretty cool which is why we do salary's
don't count.
We get the total length of the array as our maximum number and then we run through it.
So we know it's going to stop once it gets us where we need to be.
So we don't have to do this kind of junk anymore.
We don't have to do this kind of junk anymore.
You're going to have an array of salaries and then you can run loops to get information out of the sellers
or out of that array which is pretty cool.
Now loops can take a long time to understand if this is really confusing for you.
It's OK.
This is it's normal to be confused.
All you need to know as a beginner is how to use these tools because as you get better at them you're
going to get you're going to understand what's going on under the hood and how these things work.
OK so what we've done here is we've created a while loop and we've created three four inch loops.
There is one more loop I want to talk about and it's a For Each loop.
OK.
And it's technically a four in loop but I'll show you why it might make sense to Word.
As for each so for salary and salaries they print salary.
So let's say you want to create something that goes for all the salaries and prints them up.
OK.
So what we're doing here is very similar to this for in loop.
But there's no range.
OK.
So what we're doing here is we're saying for each.
Visualize this in your mind for each item in this array.
Run a loops.
That's the condition for every item that's in this array.
Run this loop.
So in our case here for every salary that is inside the salaries array.
OK print salary so what it does is it skips some things for you if it takes off this heavy lifting for
you.
Grabbing the index it says don't worry about that.
We'll do that for you.
So it grabs the item out of the array for you automatically and sticks it here in it'll Konst it.
How cool is that sticks for you right there ready for you to use without having to grab it out of the
out of the array itself.
And then we just we're done in this case we're just printing it right.
Pretty good stuff I think.
And so it's doing the exact same thing as you can see right here.
It's printing up all of these salaries here.
And so this is a for loop as well but may also want to call a For Each loop ok for each salary in salaries
grabs each item out of the array.
One at a time puts into this variable so you can use it and when it gets to the end your loop is done.
So you don't ever have to worry about an infinite loop.
For the most part in this case whereas right here we had to create some condition to get rid of it to
get out of the loop.
OK.
So really cool stuff loops.
This is what you're going to do most of the time there's a lot more that goes into it and they get trickier
as you go down the line.
But for most of what we'll be doing and for most of what you'll be doing this kind of covers the basics
of what you spend most of your time working on when it comes to loops and arrays.
So that's it.
Mark Price of slopes of dot com.
See you next time.