A computer does exactly what it is told — so learn to tell it exactly
Short introduction and objectives.
Objectives
- Explain what a program, an algorithm and a programming language are
- State the properties of a good algorithm and the steps of problem solving
- Recognise sequence, selection and iteration, in pseudocode and in flowcharts
- Explain the difference between compiled and interpreted languages, and where C, Java and Python sit
Ask a friend to explain how to get from Mutengene motor park to the Buea council. They will say "go straight, then turn at the big junction". You understand, because you know which junction is big. A computer does not. It cannot guess, it cannot ask, and it will never fill a gap with common sense. Programming is mostly the skill of saying exactly what you mean. The language — C, Java or Python — is the smaller part, and you will learn three of them in this course.
Program, algorithm, language
- An algorithm is a finite, ordered list of exact steps that solves a problem. A recipe is an algorithm; so is the way a cashier gives change.
- A program is an algorithm written in a programming language so a computer can run it.
- Code is the text of the program. A bug is a mistake in it. Debugging is finding and fixing bugs.
A good algorithm is…
- Finite — it stops.
- Definite — each step means one thing only.
- Has input and output — it takes something in and gives a result.
- Effective — each step can actually be done.
- Correct — it gives the right answer for every allowed input, not only the example you tried.
Solving a problem in four steps
- Understand it: what are the inputs, what output is wanted, what are the rules? Write an example by hand.
- Plan it: write the steps as pseudocode or draw a flowchart.
- Write it: turn the plan into code (from week 3).
- Test it: run it by hand with a trace table, with normal values, boundary values and wrong values.
Only three building blocks
- Sequence — steps one after another.
- Selection — a decision: IF a condition is true THEN do this ELSE do that.
- Iteration — repeat: WHILE a condition is true, or FOR each item in a list.
- Every program ever written, from a calculator to Mobile Money, is built from these three, plus variables: named boxes that hold values.
Pseudocode and flowcharts
PSEUDOCODE CONVENTIONS USED IN THIS COURSE
INPUT x read a value into x
OUTPUT x show x
x <- 5 put 5 into x (assignment)
IF c THEN ... ELSE ... END IF
WHILE c DO ... END WHILE
FOR i <- 1 TO n DO ... END FOR
FLOWCHART SYMBOLS
( oval ) Start / End
[ rectangle ] a process: x <- x + 1
/ parallelogram / input or output
< diamond > a decision with Yes and No exits
-----> arrows show the order
From your algorithm to a running program
- A compiler translates the whole program into machine code before it runs. C is compiled: fast, close to the machine. You meet it in weeks 3 to 6.
- Java is compiled to bytecode, which the Java Virtual Machine runs on any computer. It is built around objects: weeks 7 to 9.
- An interpreter runs the program line by line. Python is interpreted: quick to write and test. Weeks 10 and 11.
- The algorithm you design does not change from one language to another. That is why this course starts here.
The uncomfortable part
Beginners want to start typing code on day one. Those who do spend weeks fighting errors in programs whose logic was wrong before the first line. Two weeks on paper feels slow. It is the fastest route there is.
Where beginners go wrong
- Steps that a human understands but a machine cannot: "add the tax", "check it is correct".
- Testing only the example from the question, never the edge: exactly 5 000, zero, a negative number.
- A loop with no way out.
- A decision diamond with only one exit.
- Using a variable before giving it a value.