top of page

let's talk about four loops. So I'm going to comment this so it doesn't keep running in the background and for loop is kind of the gorilla in the room. So for those are used in my experience a lot more often than while loops or do while loops for loops are used all the time and they're just really easy to work with. Once again when things kind of wrap your head and learn how to use of loops from a digital marketing company in Hyderabad around what they're doing and what a for loop does is it will repeat until a specified condition evaluates to false. So it's pretty similar to while loop. It does have important differences though in terms of how you define it. And basically, it looks like this. Let me put it in two comments here.


So you're going to use them for keyword and then it takes basically three arguments. It takes an initial expression semi-colon takes a condition to break out of the loop it takes in increments expression. And then typically you put in curly brackets whatever statement you want it to loop over and over. So it's functions like I said similar to a while loop in digital media agency but it's a little different in terms of how it's defined. So you say for this is the initial expression you want it to use. This is the condition it evaluates for every loop iteration and then this is how you want it to increment whatever expression you initialized it with. And then usually in curly brackets, you put in whatever logic you want x. Right.


So that's going to look something like this because of a variable or a lot Konst wouldn't really work. And we'll see why in a second but let's say we have a variable just for old times sake. Var index equals zero. For example, search for the best digital marketing company in Hyderabad then we could say for index What condition do we want. Let's say we want to loop times.


For example. Right. So we want to loop times. So we want to go until the index is less than which is basically saying once the index is or greater We want to break out of the loop. And then how do we want to increment. Well, we're going to do index plus equals of and let's see what happens. Console log index and it's all clear. We'll look for a digital marketing company in Hyderabad. And we see that it looped through ten times. Right one-two. All the way to because it starts at zero. Now, what if our logic was a little bit backward let's say we did minus equal.


Let's observe Let's see what happens. Run and it looks like my browser crashed right. I'm infinitely looping because it's never going to exit out of the loop because the index is always going to be less than. It starts at zero and it's just going higher and higher. Right now my browser is unresponsive. I'm going to have to wait for it to give me that pop up saying hey should I kill this or should we wait. And again it's not going to take much time for a best digital marketing company in Hyderabad to break anything. And that's how we learn. But we can see it is different from a while loop just executes until you tell it to stop for a loop you tell it where to start and where to start basically so you know I'm going to kill it. And then we will just pretty fresh this page and should reload.


OK. So now we want to make sure that our break condition is correct. We try again. There we go. So now I'm not looping infinitely anymore but this is the kind of thing we're going to need to be aware of. Another big difference is that we don't increment inside the actual loop we put it inside you know the four keyword initialization section. We're doing initial expression from a digital marketing company in Hyderabad condition increment expression. We can pass a variable as we've done so. It's also very very common to see it like this. Var index equals something you know you can initialize it inside the for loop and even work communist's to see something like this.


The bar I equal this and I usually just stands for index and it's just kind of common to see so I'm going to try to stick to conventions and use that here. But this could be anything it doesn't have to be index you could name it whatever you want. It's just kind of a convention to see I in there in the place of any other variable and at its core from a digital marketing company in Hyderabad, it's pretty simple. We're just telling the computer to do this thing as many times as we needed to do it. And in this case, we're doing it times. We could do greater than or equal to as well in which case it'll do times right if you count these out. It's going to result in.


We could have started out one as well and then just looped until it's greater than or it is less than it's let me clear that and run it again. There we can start at loop all the way to. It's just kind of try to wrap your head around that makes sense to start at zero because then we can just be kind of clear in our intentions here in the actual loop condition from a digital marketing company in Hyderabad. And if we want to see a similar example from before let's say we have an array of different types of ice cream for example on Ice cream ice creams and it's an array with a bunch of strings inside of it. So we have no chocolate rocky road strawberry. And we wanted to print out each of these ice creams and if we weren't using a for loop Well it would be kind of annoying to do that.


Right we have to cancel the log. Ice cream at the position. Cancel a log. Ice cream at position et cetera et cetera. Or if we cancel all that out ice cream or ice creams you get the idea. It's pretty tedious to have to do that. Thankfully the computer can do that for us. This is the kind of thing that computers are really good at unrated it's counting and printing out numbers. So if we do that here instead of log ice creams at any given index we're on we are going to be able to do that.


Now, this might cause an error because we're trying to count more ice creams than there are undefined for a bunch of them. So again if we kind of applies the logic we applied earlier to this one. Well, ice creams have a definite length. One two three four. So we could just put that in go into. You know I and I had less than four but digital marketing company in Hyderabad it makes more sense to basically do something like this. Ice cream length. Right. This is for. Because it's basically counting it for us. We don't have to worry about it. You know the less you have to hard code this stuff in the better it makes your code a lot more reusable. So that definitely works. If we want to rewrite this to be a similar function to what we did up above here name Luper we can easily do that. Let's do it. Why not.


This one will be ice cream whooper function ice cream Luper takes in an array ant type of ice cream and cut that paste it down here. All right. So we're going to go until we reach our length and we'll cancel log type. So if we invoke ice cream Luper And you know what. Forget about tape for now. This is more of a sanity check. So we'll grab the array at any given index with the ice creams you want to loop over. Let's try it out. OK. Same functionality if we want to be able to loop through and grab a certain type so we can just call it T or whatever Pyr type again you can throw an if statement in here. So if are at any given index is equal to the type we're looking for.


Console log that one because we loop through each of these and that's all we really need to do. Let's try to yes we need to give it a type ice cream. Let's look for vanilla cake. Clear run. He looks like we got it and probably wants to log digital marketing company in Hyderabad what index that was that do the string for newer space actually is at position plus whatever index we're on at the time. The right index we use a lot with loops because you know it's all numbers at the end of the day. So we got an error. What's the error. I expect the token and I think it's a plus here. Yeah, will probably do it. But it is at position make sense whereas our rocky road run cool.

bottom of page