Control flow and logic, part II: two types of loops · With while loops, you must be a little more...

72
With notes! 1

Transcript of Control flow and logic, part II: two types of loops · With while loops, you must be a little more...

Page 1: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

With notes!

1

Page 2: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

2

Page 3: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

3

Page 4: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

Sorry for that annoying problem set problem last time, but I wanted you to really feel the need for loops!

4

Page 5: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

5

Page 6: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

First we'll talk about for loops.

6

Page 7: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

7

Page 8: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

8

Page 9: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

Note the indentation, much like with if/else statements, is very important and defines what belongs to the for loop. Note that "var" can be any variable name you want, and you do not need to define/declare it before using it here.

9

Page 10: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

10

Page 11: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

More about how range() works in a minute, and more on lists next time.

11

Page 12: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

12

Page 13: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

13

Page 14: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

14

Page 15: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 16: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 17: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

17

Page 18: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

This is a basic counter. Kind of pointless in this particular example, but overall a very very useful thing. Remember it!

18

Page 19: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

19

Page 20: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

20

Page 21: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

21

Page 22: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

22

Page 23: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

23

Page 24: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

24

Page 25: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

25

Page 26: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

Kind of a dumb example, but a very important concept.

26

Page 27: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

It may seem a little odd that it goes from 0 to n-1, but since most things in programming start counting at 0, it ends up being more convenient this way. We'll learn more about lists next time!

Page 28: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 29: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 30: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 31: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 32: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 33: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 34: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

34

Page 35: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

35

Page 36: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

How is this different than a for loop? Unlike the for loop, you don't need to specify exactly how many times the loop will run. Again, the indentation is what defines what is part of the loop

36

Page 37: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 38: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 39: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 40: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 41: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 42: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 43: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 44: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 45: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 46: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 47: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop. An endless loop occurs when your conditional never becomes False, thus causing you to never exit the loop.

Page 48: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 49: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 50: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 51: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 52: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 53: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 54: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 55: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 56: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 57: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 58: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.
Page 59: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

You can usually use either type of loop, but one will feel a lot more natural and be easier to code. That's the one you should go with.

59

Page 60: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

60

Page 61: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

61

Page 62: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

62

Page 63: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

63

Page 64: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

What open() technically does is create something called a File object, and this is what we store in the variable "inFile". This file object is an iterable object, so we can use a loop on it. The individual iterable units are lines in the file, so during each iteration a single line is assigned to the loop variable. The lines will always be read from first to last.

64

Page 65: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

65

Page 66: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

66

Page 67: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

67

Page 68: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

As you may have noticed, the print command automatically inserts a \n after it prints everything (if it didn't, you'd have everything running together on the same line when you use multiple print statements).

68

Page 69: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

You can pretty much use this as a template for most file reading situations. The main part you'll be changing is to replace print "Line:", line with something more interesting/useful

69

Page 70: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

70

Page 71: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

These methods can only be used on file objects. The syntax for using them is similar to what we've seen with modules, e.g.: inFile.read() inFile.readline() Where "inFile" should be replaced with whatever variable name you assigned to your file when you opened it.

71

Page 72: Control flow and logic, part II: two types of loops · With while loops, you must be a little more careful than with for loops because of the possibility of creating an endless loop.

72