Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
So let's talk about iteration. This is the programming buzzword for a loop. And here's the idea. You start writing some code. A few statements that deal with, say, looking in all the MP3 files in a folder or changing the position of an image on the screen. So we write some statements to do it once. Then we figure out we need to do it again. So we write them again and again and again. All these can get pretty tedious and rather than write it out several times or even several thousand times, I'd like to be able to write my statements once and then just say "Repeat this." Repeat it 5 times or repeat it 5,000 times, or keep repeating until told to stop, or go through all the fields on a web page or go through every MP3 file in a folder.
So we might repeat two statements, we might repeat a dozen, we might repeat a hundred. And this is what we do by creating loops and you'll also hear this called iteration. Now creating loops is actually easy. The main issue with any loop is not when to loop. It's when to stop. If we need to loop 5,000 times, who's keeping track? If we need to loop until told to stop, well, where's the code to figure out how to stop? So all our loops will actually have conditions that control how long they loop.
So how do you do this? Well, we've seen an if statement. If the condition is true, do whatever is in the block. In this case here I have got a variable a = 1. If a < 10 we pop up an alert box. Classic if statement here. Well, simply replace the if with the word while and we have a loop. Not if this condition is true, but while this condition is true, execute the body of this code.
It could be one line in this block or a hundred lines. And whatever is in the body of the loop gets executed for each iteration, meaning every time the loop goes around. So here is what happens. We set off setting the variable a = 1 and we are storing that value in memory. We then jump down to the while and we check the condition just like with an if statement. Here, it evaluates as true. Yes, a is less than 10. So we jump into the body of the loop inside the curly braces and we execute whatever code is there, in this case just popping up an alert.
Then we get to the end of the loop. The end of the loop is marked by the closing curly brace of the while statement. What will happen here is we jump back to the start of the loop and we check the condition again. And this happens automatically. Is a < 10? Well, yes it is. So we move on and we go on again. Now here's the problem. If nothing in this block changes the value of a, and a is what we are checking, we will now have an infinite loop.
And an infinite loop is a very bad thing. We would now be stuck in this loop forever. Or at least until we turn our computer off or force our program to terminate. So in this loop and in most loops we are needing to keep track of something. In this case, we need to add a line to increment the variable a. So if I add an a++, and that's inside the body of the loop, then when we are going around this one again we will check is a < 10, yes, it is, we pop up the alert, we do whatever other code is here, and right before the body of the loop closes, we say a++.
Meaning we add one to the value of a. a is now 2. We come back up and we will check it again. Is a < 10? Yes, it is. We'll jump back into the body loop and we'll keep going, but we are incrementing a. And at some point a will be 10. Then we ask it again, is a < 10? No, it isn't. It's 10. That's not less than 10. That is 10. So we count as false. Then we will jump out of the loop or we'll jump right to the end without executing any of that code and we'll move on and we'll execute whatever code comes after.
Get unlimited access to all courses for just $25/month.Become a member