Viewers: in countries Watching now:
Finally, the course compares how code is written in several different languages, the libraries and frameworks that have grown around them, and the reasons to choose each one.
So I'm going to write some simple iteration code here, but before I do I have a quick question for you and this is not a trick question, it's not a riddle, but I am interested in the first answer, your gut answer that comes to mind. Here is the question. You need to build a straight fence and it must be 50 meters wide and you need fence posts every 10 meters. How many fence posts do you need? Do you have your answer? Good! Let's get into the code.
So these are just comments. Let's create an index here. It's very common to call your index i. But you can call it whatever you want. So we'll start off with an index of i = 1. Now I'm going to come and do my while loop. Again it takes the same format as ifs. We've got the word while then our opening and closing parentheses and then our opening and closing curly braces that represent the body of the loop. So in while we got the condition that we're going to check. Just like an if statement that needs to be either true or false.
And what I'm going to say is while i < 10 we're going to jump into the loop. What I'm going to do is add 100 to amount, which I could say amount+100. I could have also used the += format here, and then what I need to do inside the body of the loop is here is where I need to increment the index. I can't wait till I leave the loop, because if I do we'll never hit that line. The increment must be inside the loop itself.
I'm just going to say i++. So every time around we're going to add 1 to the variable i, we'll then hit the end of the loop, the closing curly brace, and we'll jump back up to the while statement and check the condition again. Now finally what I'm going to do is use our good old alert box and say "The value is: " and we'll write out the final value. We're only going to hit this line after we're done with the loop and we write out the value of the variable called amount. So I'm going to save that and test it. So we're going through this, we're going to add 100 to amount 10 times which should give us 1000.
But no, we get 900. This is a very common mistake to make and some of you I'm sure have spotted the problem already. I started off with my index set to 1 and then I started the loop. Is i < 10? Sure it is, it's 1. We then go through the loop, we add one to it. It's now 2. Is i < 10? Sure it is, it's 2. We add another hundred, we keep on going. Now at some point i will go from 8 to 9, we'll loop around again, and then it will go from 9 to 10 and we'll loop around again.
We'll check the condition. When i is 10 this will evaluate as false, so the last time we go and run the loop, we run i = 9. So we're only actually adding 100 nine times, not ten times. Now what could have worked here is either saying if i <= 10, or we could have started off with our index set to 0, because that would have looped around once before we even incremented it to 1. But of course the flip side problem here is if I did both and we try and save that and run that page again, we're now going to end up with 1100.
So I want one solution or the other, but certainly not both. Now these are what is known as off-by- one errors and it's really easy to go around either one too many times or one too few if you don't have it exactly straight in your head. And it's not computers causing this issue. The issue is with us. It's an easy mistake to make even without computer is involved. One example of this is known as the fencepost error. This is the question that I asked earlier. You need to build a fence. It needs to be 50 meters wide with fencepost every 10 meters.
How many fenceposts do you need? Now lot of people's gut reaction is 5, some say 7, but the answer of course is 6. And if it's this easy to make off-by- one errors when we're working with really simple concepts like 50 divided by 10 or 100 times 10, how do you think it is when you're dealing with way more complex data with much more abstract start and end positions? And this is just one example. When we start working with collections of data making sure that we're going through it exactly, it's quite easy to get wrong.
Like with the fencepost error, every programmer I've ever known will admit to making off-by-one errors. So a little extra thought with your loops can go a long way.
Find answers to the most frequently asked questions about Foundations of Programming: Fundamentals .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.