Introduction to programmingCreating a program from an algorithm
Programming is writing computer code to create a program, in order to solve a problem. Programs consist of a series of instructions to tell a computer exactly what to do and how to do it.
Consider this simple problem. A cinema is offering discount tickets to anyone who is under 15. Decomposing this problem, gives this algorithmA sequence of logical instructions for carrying out a task. In computing, algorithms are needed to design computer programs.:
find out how old the person is
if the person is younger than 15 then say “You are eligible for a discount ticket.”
otherwise, say “You are not eligible for a discount ticket.”
In pseudocode Also written as pseudo-code. A method of writing up a set of instructions for a computer program using plain English. This is a good way of planning a program before coding., the algorithm would look like this:
OUTPUT "How old are you?"
INPUT User inputs their age
STORE the user's input in the age variable
IF age < 15 THEN
OUTPUT "You are eligible for a discount."
ELSE
OUTPUT "You are not eligible for a discount."
To convert the flow chart (also known as a flow diagram) or pseudocode into a program, look at each individual step, and write an equivalent instruction. Sometimes the steps will not match exactly, but they will be fairly close.
In a flow chart, this algorithm would look like this:
Creating the program in Python
A PythonA high-level programming language. (3.x) program to meet this algorithm would be:
age = int(input("How old are you?"))
if age < 15:
print("You are eligible for a discount.")
else:
print("You are not eligible for a discount.")
Note the similarities between the flow chart and pseudocode, and the finished program.