How to Program, Part II
Normally python executes each line of your program in sequence, from the first line to the last line. Sometimes this is not flexible enough, and for this reason we introduce "control flow."
Control flow allows us to cause the program to do something conditionally, or to do it multiple times.
Here is how we make python count:
This is called a "for loop" because the program loops over the same piece of code (line 2). The print statement executes 4 times, taking on each of the values from 0 to 3. You could make it start at 1 if you wanted, all you'd have to do is this:
Question: How do you make Python count by 5's?
What if we want to make python add the numbers up as it counts?
Something important to notice here is what happened on line 1. Here we "create" or "define" the variable s. Without this line, we would not be able to add to the variable s later.
Note that there are spaces in front of the print statement in the programs above. This is important. The spaces tell python that the print command is inside the for loop. Here is another program with more print statements. This should clarify what is inside the loop and what is not.
When you run this program you will see that the "in loop" message gets printed several times, but the "before loop" and "after loop" each get printed only once.
Another kind of control flow is the "if" statement. It works like this:
This program will do nothing at all. With a slight change:
Now it will print out "x is more than 2".
But there is yet another thing we can do:
In the above code "elif" is a kind of short-hand for "else if". If the initial "if" is false, then the program checks the first "elif". If that is true, it finishes evaluating the sequence of if/elif's. If the result of the first elif is false, the program proceeds to the next elif, and so on. If all of the elif's fail, the "else" is executed.
This program can produce a number of different outputs depending on what value is assigned to x on line 1. If x is 6, then it prints out "x is six". If x is 2, it prints out "x is less than or equal to three". If x is 5, it prints out "x is 5".
You can combine these control flow structures in arbitrary ways. Consider this program:
This program enumerates the number of ducks, and uses the correct ending in each case. Note that "i%2" means the remainder after dividing i by 2. If the remainder after dividing by two is zero, i must be an even number. The above program gets really excited if it sees an even number of them!
Something that may be helpful here, is that you can use "pass" to have no action take place.
Here I've thrown another curve your way. Notice the "and" between the two conditions "i >= 2" and "i <= 3". This if block is only executed if both conditions are true. Alternatively, I could use "or" which will work if either condition is true.
Up until now, the loop variable for all the loops we've looked at increased by 1 at each step. It's possible to ask it to increase by two, three, (or even by -1).
Range starts with the first number, goes two the second number, and increments by the third number. Thus, the above loop starts with 0, stops before 16, and counts by 2.
Another kind of loop is the while loop. You can count using the while loop, but it requires a bit more typing.
This produces the exact same result as count.py above.