| How to Program, Part IINormally 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 zero to 3. You could make it start at 1 if you wanted, all you'd have to do is this:
or 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 isinside 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 will produce a number of different outputs depending on what value is assinged 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. It gets really excited if it sees four 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.
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.
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||