Thursday, October 16, 2008

Chapter 4 Conclusion

In the last post, I covered everything from Chapter 4 except for loops. There are two types of loops that we learn about in Chapter 4. These are “for…in” loops and “while” loops.

In a “while” loop, the operation will first check and see if it’s test condition is True. If it is, it will evaluate the indented statements. When it is done, it will go back and see if the test condition is still true. If it is, it will continue going through this process until the test condition returns False and then the program will move on.

A “for…in” loop is similar, but has fewer steps. In the first part, you give a name that you’ll use inside the indented code. In the second part, you give a sequence which takes each element and assigns the value of the element to the name you provided in the first part.

I don’t completely understand what the book is saying exactly and it just sort of throws in the pop method in the “while” loop without really saying what exactly it does.

Sometimes in writing these loops you run into an infinite loop that doesn’t stop. In order to stop this, you can use “break” at the end of the indented section.

Both “for…in” and “while” loops can have an else statement, as long as they’re not stopped by a “break”. This is used when the test condition is False and allows an alternative event to take place.

Another feature that is used a good bit with loops is “continue”. Instead of terminating the program like “break” does, “continue” just tells the loop that you want to skip the rest of the current repetition of the loop.

This concludes Chapter 4.

No comments: