• Calculus of Control Flow by Digital Marketing Company in Kolkata



    Now we're going to get into four loops. And like I said loops couple really well with arrays. So why don't we use that example? So I'm just going to create a simple number array here is just going to call this Nahm array. OK. And it's just going to be a list of a bunch of numbers. I'm going to answer in some random values here. Nine Okay that's good enough as said by digital marketing company in kolkata and these are just completely arbitrary values.


    So my goal here is now to go through this array and double each of these and then prints out the doubled value. A simple enough task that we could actually accomplish just without the use of any kind of loop. We could then say something like Prince Nahm array or for example times. Ok, that's going to print out my first number multiplied by. And that's what we want and I could just do this five different times for each of the elements here. But first of all, that's a very impractical way and inefficient way of doing things.


    And second of all what if my array was like a hundred or thousand elements long well then that's a thousand extra lines of code we have to write which would take ages to write and is just horribly ugly and inefficient. Luckily for us, we can actually write pretty much this exact line of code put it in a loop and let the loop do all the work for us. Now if we're clever we can actually do the same thing as did by digital marketing company in kolkata and others. We can do this with a while loop. We will just have to create an extra indexing variable but it's much easier with a for a loop. So I'll show you how to do that.


    We start off with a. Ok and now we'll follow it with some interesting since X so just bear with me here and I'll explain what's going on. Once I finish writing this. So in actual let's say this for Nahm an array. OK. We are simply going to print out and actually, I'm just going to take this and just paste it right in here. But I'm going to change some array of to array of times to oh and this should be numb. There we go. So now it's complete. Now, this is kind of weird syntax. If you are not used to it so allow me to explain what is going on.


    So we're basically paying for something in Namah right and now there's a saying for each of the elements within array when it comes to that element we're going to call it numb. So at the beginning and then, we're going to execute this on each of those at the beginning. Nahm is going to jump to the first element in this array. Because this is a for loop and that's how they work. So numb would take on the value of because this is saying is the first element. OK, we are at the beginning of the loop and the array.


    So Nam is going to equal and now we're just going to print. And not sure why we went for index's should actually just be Nahm times. Apologies on that faulty syntax there. OK. So this is just going to take a value Nahm currently holds. Time's up for digital marketing company in kolkata and then print it out. Now once we finish up we'll get to the second value in number which is again. Now will equal will Prince times to then the third value. Nahm is to print that turns to numbers sixty-seven. Then Number nine. And then after that, we say AK Nahm array is finished.


    There are no more elements in it. So then we go into X out of this loop. So this is why it works so well with arrays and lists is because we can simply say for each of the elements within this array or list we going to call this variable and have it take on the value that we're currently on in the array and then we're just going to do something with it in the loop. So if I were to execute this I get these numbers being printed out. I start with and. Then for the then so it's essentially exactly doubled. Each of these numbers. Now the great thing about four loops versus something like a while loop is I know exactly how many times it's going to run.


    Also, I know it's going to start at the beginning of course from digital marketing company in kolkata this time. It's going to end at the end of my array and it's going to visit every single element within my array. Now that's not the same for while loops we have to design the code very carefully when doing essentially the same thing in a while loop.


    And that's because we might not necessarily know where to start in the loop. We might not know where the loop ends and maybe again we'd have to create some kind of an indexing variable. This, on the other hand, is kind of doing all of that for us. It's saying OK so numb is equal to Naama array of. We're going to do that then numb is equal to the number of one that will do that. Then two and three then four.


    And that's it I guess or any five elements. All right. So those are basics of flips just a really handy way to visit each of the elements within an array and then do something based on that. Now it's similar to our IF statements where we had nested if statements as in digital marketing company in kolkata program. We can actually have nested loops. So if I had like an array of arrays and I wanted to iterate through that probably use nested loops to do so but nested loops can get a Lessel complex and can be very slow. So as much as possible choice avoid doing that. However, in some situations, it is just the only way we can do solve the problem. Otherwise, that is your basic loop intro.