Programming constructs - CCEAProgram Flow

Computer programs use data stores to organise the different types of data they contain. Data stores can be either a constant, variable or an array, and contain inputs, outputs, functions, instructions, sequences and more.

Part ofDigital Technology (CCEA)Digital development concepts (programming)

Program Flow

Sequence

simply means executing each instruction once, in the order it is given.

Example:

Asking for a user's name and printing it on screen

name = input("What is your name?\n")
print("Hello", name)

Outputs:

What is your name?: Mike
Hello Mike

The program was executed one sequence at a time.

Selection

There are times you don't want to execute everything in a sequence. You may want the program to which set of instructions to execute based on certain conditions.

We can use to test a variable against a value and act in one way if the condition is met or another way if it is not. This can be represented as:

IF (Condition evaluates to True): DO THIS
ELSE: DO THIS OTHER THING

Example:

name = input("What is your name?\n")
password = input("Please enter password: ")
if name == "Mike": print("Hello Mike")
if password =="neuromancer": print("Access Granted")
else: print("Access Denied - Wrong Password")

Outputs:

What is your name?: Mike
Hello Mike
Please enter password: neuromancer
Access Granted

or

What is your name?: Mike
Hello Mike
Please enter password: countzero
Access Denied - Wrong Password

IF statements

IF statements are very useful if there are two possible outcomes. However, if your program needs to evaluate multiple conditional statements, you can use:

IF > ELIF (or ELSE IF) > ELSE

Example:

name = input("What is your name?\n")
age = int(input("What age are you? "))
if age < 13: print("You are a child")
elif age >12 AND age <20: print("You are a teenager")
else: print("You are an adult")

Outputs:

What is your name?: Alice
What age are you?: 12
You are a child

or

What is your name?: Alice
What age are you?: 13
You are a teenager

or

What is your name?: Alice
What age are you?: 20
You are an adult