4

Chapter 4

Variables and Control Flow


There’s nothing you can put up in front of them that will discourage them from going on.

Lynda Plettner, talking about her dogs in the 1999 Iditarod

Figures

Math with longs
Figure 1.
Printing
Figure 2.
Printing
Figure 3.
If statements
Figure 4.
More if statements
Figure 5.
For loops
Figure 6.
range()
Figure 7.
For loops and if statements
Figure 8.
The while() statement
Figure 9.
More while()
Figure 10.
Syntax error; can’t assign in a control statement
Figure 11.
Breaks
Figure 12.
Breaks and continue
Figure 13.
Pass
Figure 14.
Things you can do, but shouldn’t
Figure 15.

Example Programs

There is one example program for chapter 4: download chapter4.zip.

Exercises

Re-think the complete leap year program given in Listing 4.1, and see if you can come up with a better way to write it that would use while instead of for. There’s no point in re-writing code, though, if you don’t see an improvement.

You could also search on the net for references to the very earliest computers, and see if there’s any information about programming languages for them. See what logic control statements were available for these early languages and computers.


Valid HTML 4.01 Transitional