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.

Part of Computer ScienceProgramming

Creating a program from an algorithm

Consider this simple problem. A cinema is offering discount tickets to anyone who is under 15. Decomposing this problem, gives this :

  1. find out how old the person is
  2. if the person is younger than 15 then say “You are eligible for a discount ticket.”
  3. otherwise, say “You are not eligible for a discount ticket.”

In , 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:

A flowchart can be used to determine your age and whether that makes you eligible for a discount.

Creating the program in Python

A (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.