‫Narrator: Hi.
‫Within this lecture we're going to start
‫learning about loops.
‫So we are going to see what are loops
‫and how we can leverage them.
‫But first I want to create a new Java file,
‫over here so that we can have a clean page to start upon.
‫So I'm gonna name these loops, okay.
‫Of course, we're gonna have to add our main
‫method here as usual.
‫So remember how we do that?
‫Yep. Public and void main.
‫And we are gonna have to have string array of,
‫name of argument and open curly brace.
‫So here you go.
‫This is our entry point for our Java code.
‫So what are loops and how do we use them?
‫So first of all, there are more than one kind of loops.
‫So first we're gonna start with for loop.
‫Okay?
‫But loops in general actually enable us to
‫do repetitive operations,
‫as long as some condition or some range holds.
‫For example, in for loop,
‫we're going to do an operation, repetitively,
‫as long as we are in a range.
‫And this is very suitable to use with arrays.
‫And you will see with arrays and arrayals,
‫most of the time we are gonna have to use for loops,
‫because it makes sense.
‫So let me show you what I mean.
‫Let me create an integer array and I'm,
‫I'm gonna call this my numbers,
‫and I will just open the curly braces,
‫and give some random values like 12, 15, 18, 21, and 24.
‫Okay? This is my for, this is my integer array,
‫and I'm going to use for loop for some reason.
‫And that reason is, for example, let's assume
‫that I have an X and I'm driving this X out of any number
‫inside of my number array,
‫and I want to divide this by three
‫and multiply the result by five.
‫So it's easy, right?
‫If I say my number's zero,
‫it's going to bring me the first element of that array,
‫which is 12. It's going to divide it by three,
‫which is four multiplied by five, which is 20.
‫So that's cool.
‫So what if I want to do this
‫with every element inside of this numbers array.
‫So you may think that, yeah
‫create new integers and just do it.
‫My number zero, my numbers one, my numbers two,
‫just do it until the end and you will be done.
‫And yep, this might be true
‫for this case since we have five elements over here.
‫But what if I had like 100 elements,
‫and what if I didn't even know what kind
‫of element size I'm going to get?
‫Maybe I'm downloading these numbers from the Internet,
‫then this wouldn't work fine for me.
‫So this is where we need loops.
‫So with for loop, we write for,
‫and then we give some conditions.
‫So I'm going to explain all of these parameters,
‫but right now I'm just gonna write it
‫so that you can understand it in a better way.
‫Okay?
‫So we are going to take each element out of my numbers,
‫and do some operations with that element.
‫And we are going to do this as long
‫as we are inside of the range of my number elements.
‫So let me write this and I will explain to you.
‫So I will create an integer, which is zero over here.
‫Okay? I'm doing this inside of the parentheses.
‫You may write with me if you want.
‫I'm going to say I is less than my numbers dot length.
‫Okay?
‫And I will say I plus plus and open curly braces.
‫So what did I do?
‫I'm going to explain all the parameters.
‫But first of all, we have created the coding block. Right?
‫Like we did in the if statements.
‫Now whatever I write between these curly braces
‫will get executed only for for loop.
‫Now let me come over here.
‫So I've created this integer, which is a zero. Okay?
‫And this loop will continue, as long as,
‫this integer is less than my numbers dot length.
‫And every time it ticks, it will add one to the I.
‫Now I know you didn't understand it,
‫so let me show you this way.
‫I'm gonna create a new integer.
‫Actually, I'm going to copy this,
‫or cut this and paste it over here.
‫Okay.
‫So we are creating this X, right?
‫And rather than saying zero, what if I say I over here?
‫So what do we think will happen?
‫I will tell you what happens.
‫So this will take my numbers zero first and print it
‫out because I created I with initial value of zero. Right?
‫So this will take the first element out of this,
‫my numbers array and it'll divide it by three
‫multiply it by five, and then print it out.
‫So it will check the conditions that I have given over here.
‫It will add one to the I,
‫and it will check if I is still less
‫than my numbers dot length.
‫So my numbers dot length is five, as you can see.
‫So this will continue as long as I reaches five.
‫So I will only take 0, 1, 2, 3, and 4 values.
‫And this is exactly what we need
‫because we can reach the elements with my numbers zero,
‫my numbers one, my numbers two,
‫my numbers three, my numbers four. Right?
‫So this will continue as long as
‫I is less than my numbers dot length.
‫And also this will add one I to each time.
‫Each time this loop goes on, I will increase itself by one.
‫So that it will start with zero, it will go up to one,
‫it will go up to two, it will go up to three.
‫And as you can see,
‫each time we are creating an in integer X,
‫and we are dividing it by three and multiplying it by five.
‫And we did everything only within these three lines of code.
‫Rather than creating a lot of variables,
‫we did it with only one for loop.
‫We're going to talk about that more in the next lecture.