CPSC 100: Principles of Computer Science
Spring 1997 Week 6 Reading Guide
This is the six of the weekly reading guides for Computer Science 100.
Week 6 (May 5 to May 9)
This week, we begin the study of programming. Chapter 6 introduces a high-level language called xTurtle and uses it as a basis for discussing several important programming concepts. You should read the whole chapter. However, you will not be tested on "Bachus-Naur Form," which is discussed in section 6.2.
Programming is just the process of writing a computer program that performs some specified task. Programming style varies from hacking to careful software engineering. But some ideas are essential to any programming task. One of the most important is the use of names for variables and subroutines.
The concept of a variable is actually pretty subtle. A variable is a named memory location that can hold a value. These three components -- name, location, and value -- are all aspects of a variable. In the xTurtle language, a variable must be declared before it is used. When the computer executes a declaration, it sets aside a memory location for that variable and assigns it a name. A value can be put into that location by using an assignment statement, or by calling subroutines such as AskUser that place values into variables.
Names are also used for built-in subroutines. A built-in subroutine is provided to the programmer as a "black box" for performing a certain task. Many subroutines can be customized by passing them parameters. You should be familiar with at least the following built-in xTurtle subroutines: forward, back, moveTo, turn, face, penUp, penDown, askUser, and tellUser.
A complex program can be constructed using loops and decisions. In xTurtle, these program structures are represented as LOOP statements and IF statements.
Building a complex program requires careful planning and analysis, and a clear understanding of the meaning (or semantics) of the language you are writing in. The meaning of a statement in a program can be understood as the effect that execution of the statement has on the state of the computer. As the program runs, the computer goes through a sequence of states. This sequence of states is called a process. The program is a kind of script for the process. When you write a program, you are designing a script that will move the computer from some initial state, through a sequence of intermediate states, to some final goal state. It is often helpful to think about what intermediate states will get you from one state to another, and then to decide what program statements are required to take the computer from one state to the next.
Two concepts that can help in the design of programs are preconditions and postconditions. A precondition is simply a condition that must be true at some point in the execution of a program, before that program to precede correctly. We could say, for example, that "X >= 0" is a precondition for taking the square root of X. A postcondition is something that is guaranteed to be true after a certain part of a program has executed. For example, a postcondition of the statement "moveTo(0,0)" is that the turtle is at the point (0,0). One way to think about programming is to notice that a precondition is required at a certain point in the program, and then to make sure that that required precondition is a verifiable postcondition of the preceding section of the program. (Please keep this straight: A precondition is something you want to be true. A postcondition is something you know is true.)
Concept List for Week 6:
- Programming, hacking, and software engineering
- Turtle Graphics
- Syntax and semantics
- Built-in subroutines
- Subroutine call statements
- Variable declaration statements
- Assignment statements
- Input/Output statements
- LOOP statements
- EXIT statement
- IF statements
- Comments in a program
- Processes and states
- Preconditions and postconditions