Creating a for loop
Video: Creating a for loopThe while loop is the classic loop in C- based languages, but after you've looked at a few you'll realize that a pattern begins to emerge. That you always have to deal with the same elements. Regardless of what code you put in your loop, whether that's one line or a hundred of them, you're dealing with setting up an index to keep track of the loop and you have to do that outside of the loop itself. Then you're going to be checking the condition and then you have to make sure that you're incrementing the index inside the loop, but at the end of it.
- Where to go from here
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.
- Writing source code
- Understanding compiled and interpreted languages
- Requesting input
- Working with numbers, characters, strings, and operators
- Writing conditional code
- Making the code modular
- Writing loops
- Finding patterns in strings
- Working with arrays and collections
- Adopting a programming style
- Reading and writing to various locations
- Managing memory usage
- Learning about other languages
Creating a for loop
The while loop is the classic loop in C- based languages, but after you've looked at a few you'll realize that a pattern begins to emerge. That you always have to deal with the same elements. Regardless of what code you put in your loop, whether that's one line or a hundred of them, you're dealing with setting up an index to keep track of the loop and you have to do that outside of the loop itself. Then you're going to be checking the condition and then you have to make sure that you're incrementing the index inside the loop, but at the end of it.
So you always have these three pieces going on to have a successful loop. And this is such a common model. There is actually a statement that brings all these pieces together and that's called a for loop. It's the for statement. It's still follows that same basic if statement format. We've got the word for, we've got the opening and closing parentheses, we got the opening and closing curly braces, but there seems to be an awful lot going on inside these parentheses. Really what we have is the same three pieces that we use in any loop.
We've got the section that sets up the index, we've got the section that checks the condition, and then we've got the section that increments the index, and these three parts are separated with two semicolons. The first time we hit this loop we'll create a variable called i, set it equal to 1, then we'll check to see if it's less than 10, it is, we will jump into the loop, we'll execute all the lines in the body of the loop. when we hit the closing curly brace we'll increment the index, we'll check the condition again, and we'll keep on looping until that condition is no longer true.
The great thing about this is it's really readable. Everything about the loop is right there at the top. You don't have to look outside the loop to find the index being set up nor do you have to scan inside the loop for the increment to make sure that's in the right place. So it's a very clear and straightforward way of setting up a loop. You'll probably find this is more common in any code that you'll find online or in books. And you will find that while loops and for loops will take care of most of the manual looping that you ever need to write. And these are the same across all C-based languages.
Now there is one more loop I'm going to explain, you won't see this an awful lot, but you will run into it from time to time. It's the do or do/while statement. The difference between this and the classic while is we actually move the condition to the end of the block. The format usually looks a little weird because it doesn't match anything that we've seen before, but we still have the same elements in place. I'm setting up an index, var a = 1, then I have the keyword do. I open up the blocks, we've got our code, I've got the incrementer of a++, but the condition that I'm checking is right at the end, while a < 10.
Unlike the while statement we actually do need a semicolon at the end of the do/while statement. Now the really big difference with this format is that the block and whatever is in it will always be executed once before the condition is even looked out. So even if this condition was false, if a started off at 1000, we would execute the code in the block before we check the condition. That may or may not be what you want. So while you will see this from time to time, while loops are much more common than do/while loops.
In most loops you'd expect to check the condition before you enter the loop and it's much more readable to have the condition at the top of the loop as with the while or with a for than at the bottom, which is where you see it when you got a do/while.
Find answers to the most frequently asked questions about Foundations of Programming: Fundamentals .
Here are the FAQs that matched your search "" :
- Q: Using TextEdit with Mac OS 10.9 Mavericks?
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.