Skip to content
academia.sh

Lesson 01 / 18

What Is a Program

The input-process-output model, execution order, the distinction between statements and expressions, and error types.

Contents

The previous course established how a computer executes instructions: the processor fetches, decodes, and executes bit patterns in memory. This course looks at the same process from the other end — from the perspective of the person who writes the text that produces those instructions.

This lesson asks: what does a program define, and what parts is it made of?

Input, Process, Output

A program defines a transformation with three components:

  • Input: Data to be processed. It can come from the keyboard, a file, the network, or directly from within the program itself.
  • Process: The steps that lead from input to output.
  • Output: The result produced. It can be written to the screen, saved to a file, or passed to another program.

This course will use the same concrete problem from start to finish: finding the average and the largest value of a sequence of measurements. Because the problem is simple, every new concept can be introduced on familiar ground.

The problem’s components are these: the input is a sequence of numbers, the process is summation and comparison, and the output is two numbers.

Execution Order

Program text is read from top to bottom, and lines are executed in the order they are written. At first glance this seems too obvious to state; it has two consequences, however.

First, a value must be produced before it is used. Second, the same program run with the same input gives the same output every time. This property is called determinism, and it is the foundation of programs being testable.

Determinism has one condition: the program’s behavior must depend only on its input. A program that reads the clock, generates a random number, or pulls data from the network can give a different result on the same call. Because such sources make a program harder to test, they are restricted and kept separate from the rest of the code; the software quality curriculum treats this distinction under the heading of testability.

Order changes only through control flow structures — conditionals and loops are the subject of the next topic. The previous course showed the hardware counterpart of this: the program counter advances to the next instruction at every step, and branch instructions write a different address into it.

The First Program

The following program computes the average of two measurements:

# Input: two measurements
measurement_1 = 12
measurement_2 = 18

# Process: total and average
total = measurement_1 + measurement_2
average = total / 2

# Output
print(average)          # 15.0

Each of the five lines is a statement: it performs an action when executed. The first four statements produce and store a value; the last one writes to the screen.

Lines beginning with # are comments; they are not executed. Comments are for the reader and should be reserved for explaining why the code does something, not what it does. What the code does is already written in the code itself.

Examples in this course will be given in Python. The concepts covered — variable, conditional, loop, function — do not belong to any specific language, however; every general-purpose language has a counterpart for them. Syntax changes; the concept stays.

Running a Program

Program text resides in a file. Running the file starts the chain defined in the previous course: the text is translated, instructions are produced, and the processor executes them. There are two ways to start this chain.

Running as a file means giving the source file’s name to the language’s runner. The program runs from start to finish and then stops. In this course, the output produced will be written to the screen with print.

Running interactively means writing statements one at a time and seeing the result immediately. It is useful while learning: what an expression produces can be tried out without writing a program. Most examples in this course can be run either way.

Taking input from outside is also a statement:

text = input("Measurement: ")     # reads a line from the user
print("Entered:", text)

The input call pauses the program, waits for a line, and returns what was read as a string. Even when a number is entered, the result is a string; it must be converted before any numeric operation can be performed on it. This detail will be taken up in this topic’s final lesson.

For the sake of being reproducible, this course’s examples mostly give input directly within the code. In real programs the source of input varies; the input–process–output structure does not.

Statement and Expression

Separating these two concepts pays off throughout every lesson that follows.

An expression produces a value. 12 + 18 is an expression; its value is 3030. measurement_1 is also an expression; its value is whatever that name is bound to.

A statement performs an action. total = measurement_1 + measurement_2 is a statement: it computes the expression on the right and binds the result to a name.

The practical consequence of the distinction is this: expressions can be nested inside one another, statements cannot.

average = (measurement_1 + measurement_2) / 2      # expression inside expression: valid

A statement has no result; this is why the form x = (y = 3) is invalid in many languages. Some languages treat assignment as an expression too and allow this — a language design decision, and a well-known source of error: writing an assignment by mistake instead of a comparison produces a program that is valid but wrong.

Error Types

Programs can fail in three distinct ways, and each corresponds to a distinct stage. The previous course’s lesson on compilation stages gave the source of this distinction.

Syntax error: The text does not conform to the language’s grammar. The program does not run at all.

average = (measurement_1 + measurement_2 / 2      # unclosed parenthesis

Runtime error: The text is valid, but an operation is requested that cannot be carried out during execution.

divisor = 0
average = total / divisor              # division by zero

These errors stop the program while it is running. Where it stopped is reported along with the error message.

Logic error: The program runs, produces no error, but gives the wrong result.

average = measurement_1 + measurement_2 / 2       # 12 + 9 = 21.0  — expected 15.0

This third kind is the most expensive, because it does not announce itself. Due to operator precedence, the line above performs the division first; the result is numerically plausible and produces no warning. Only someone who knows the expected value notices.

The only defense against logic errors is independently checking the result: comparing against a hand-computed example, trying edge cases, and tests. The software quality curriculum treats the systematic form of this defense.

Program and Algorithm

One last distinction: an algorithm is the steps that solve a problem; a program is those steps written in a particular language. The same algorithm gives different programs in different languages; different algorithms can be chosen for the same problem.

This distinction serves a practical purpose: if a program does not work, one can ask separately whether the problem is in the solution or in how the solution is written. The two questions are investigated in different places.

For computing an average, the algorithm is one sentence: sum the values, divide by their count. Which language the program is written in does not change that sentence. The correctness and cost of algorithms is the subject of a separate course in this curriculum.

Summary

  • A program defines a transformation made of input, process, and output components.
  • Statements execute in the order they are written; the same input gives the same output.
  • An expression produces a value, a statement performs an action; expressions can be nested.
  • Comments are not executed and are reserved for explaining why the code is written the way it is.
  • A syntax error prevents the program from running at all, a runtime error stops execution, and a logic error silently produces the wrong result.
  • An algorithm is the solution itself; a program is its expression in a language.

Next Step

The program above used names like measurement_1 without defining what they are. How does a name get bound to a value, when does that binding change, and what happens in memory? The next lesson takes up the concept of a variable together with the memory model established in the previous course.

To keep your progress and take notes, Log in

My notes

Log in to take notes.

Start typing to search.

↑↓ Esc navigate · open · close