computational constructs

COMPUTATIONAL CONSTRUCTS
HIGHER COMPUTING SCIENCE
MR ROSS
LEARNING INTENTION
Computational Constructs Revision:
Sequence
Selection (inc. complex selection)
Iteration or Repetition
Concatenation
Pre-defined functions
REVISION – SEQUENCE
•Each instruction follows after the previous instruction.
•All instructions are executed (run).
E.g. a program to add two number together
1. Declare variables
2. RECEIVE number1 FROM keyboard
3. RECEIVE number2 FROM keyboard
4. SET number3 TO number1 plus number2
5. SEND number3 TO display
REVISION – SEQUENCE
REVISION – SELECTION
•We use conditions to write programs which can
make choices.
•This means that the program does not execute every
step from beginning to end regardless of user input.
•Simple selection means that we check one condition
only.
REVISION – SELECTION
>
<
>=
<=
=
<>
Greater than
Less than
Greater than or equal to
Less than or equal to
Equal to
Not equal to
REVISION – SELECTION – IF … THEN … ELSE
E.g. a program to validate a password
1. Declare variables
2. RECEIVE attempt FROM keyboard
3. IF attempt = password THEN
4.
SEND success TO display
5. ELSE
6.
SEND failure TO display
7. END IF
REVISION – SELECTION - IF
REVISION – SELECTION – IF … THEN … ELSEIF
E.g. a program to decide which school a pupil should attend
1. Declare variables
2. RECEIVE age FROM keyboard
3. IF age >= 12 THEN
4.
SEND secondary school TO display
5. ELSEIF age >= 5 THEN
6.
SEND primary school TO display
7. ELSE
8.
SEND too young for school TO display
9. END IF
REVISION – SELECTION – IF … THEN … ELSEIF
REVISION – SELECTION – SELECT … CASE
REVISION – COMPLEX SELECTION
•Complex selection means that we check more
than one condition.
•To join conditions we use logical operators, such as:
•AND when both conditions are to be met
•OR when only one of the conditions is to be met
•NOT when a condition is to be false
REVISION – COMPLEX SELECTION
•Example: Can you drive on your own.
REVISION – ITERATION AND REPETITION
•Two types of loop:
•Fixed – repeat a section of code a set number of times
•Conditional – repeat a section of code an unknown
number of times until a condition specified in the code is
met
REVISION – ITERATION AND REPETITION
What will the code above do?
REVISION – ITERATION AND REPETITION
What will the code above do?
REVISION – EXERCISE
Please create a simple VB program which when the user clicks on a button
will ask them to enter their age and whether they are a boy or a girl.
If the user is over 18 then tell them they are too old for school.
If the user is 12 or over and a girl tell them can go to Notre Dame
secondary school.
If the user is 12 or over and a boy tell them to go to secondary school.
If the user is 5 or over tell them to go to primary school.
If the user is 3 or over tell them to go to nursery school.
Otherwise tell the user to go home.
REVISION – CONCATENATION
•Concatenation is the joining together of 2 or more
strings
•In VB it is performed using the & operator
REVISION – PRE-DEFINED FUNCTIONS
•A pre-defined function is a function that has
already been created and is part of or in-built to a
programming language.
•A function is a piece of code which gives you a
single value.
REVISION – PRE-DEFINED FUNCTIONS
•Two parts of a function:
•name (what it will do)
•parameter (what it will do it to)
e.g. in VB UCase:
name
parameter
REVISION – PRE-DEFINED FUNCTIONS
•VB has a number of pre-defined String functions including:
Name
What it does
Example
UCase
Converts lower-case letters
into upper-case letters
Converts upper-case letters
into lower case letters
Calculate the number of
characters in a string
Extracts a sub-string from a
string
UCase(“Hello”) returns HELLO
LCase
Len
Mid
LCase(“Hello”) returns hello
Len(“Hello”) Returns 5
Mid(“Hello”, 2,3) Returns ell
LEARNING INTENTION
Computational Constructs:
Sub-programs
Scope of variables
Parameter Passing
SUB-PROGRAMS
•A sub-program (subroutine) is used by computer
scientists to describe a clear independent block of code
within a program.
•These can be run (called) from within another part of
the program.
•Used to break a complex problem down into a set of
smaller, simpler problems.
•Make code modular so it becomes more readable and
therefore easier to maintain.
SUB-PROGRAMS (CONT)
•Sub-programs are often categorised:
•procedure (which executes a set of commands)
•function (which returns a single value)
•method (name for a function defined inside a class
in an object oriented language)
SUB-PROGRAMS (CONT)
SUB-PROGRAMS (CONT)
VARIABLES AND SCOPE
•Sub-programs, functions, procedures and methods
all make use of variables.
•Variables must be declared and can then be given
a value which may or may not change (vary) during
the execution of the program.
VARIABLES AND SCOPE (CONT)
•The scope of a variable is used to describe where
within code the variable can be used.
•There are two categories of variable scope:
•local variable
•global variable
LOCAL VARIABLES
•Local variables are only accessible (able to be used)
within the sub-program in which they are defined.
SubProgram2 does not
know about age as it
was declared in
SubProgram1
GLOBAL VARIABLES
•Global variables are accessible anywhere in the code,
within all sub-programs.
LOCAL VERSUS GLOBAL VARIABLES
•The same local variable name can be used in multiple
different sub programs.
•A local variable is only created and used in memory
(RAM) while that sub-program is being run.
•When a sub-program is finished, the memory (RAM) for
the local variables is freed, so local variables are more
efficient when considering system resources
LOCAL VERSUS GLOBAL VARIABLES
•A global variable needs to be retained in memory
(RAM) for the duration of the running of the program.
•Global variables are therefore less efficient with respect
to system resources.
•Programs you write in school are small so local v. global
is not a big consideration but in real life applications
there can be 1,000s of variables so scope needs to be
considered carefully.
LOCAL VERSUS GLOBAL VARIABLES
•But what if you need to access the same variable in more
than one sub program?
•Should you just make it a global variable?
PARAMETER PASSING
•An alternative to global variables is to use parameter
passing.
•There are two methods of passing parameters:
•passing the value of a variable or
•passing a reference to a variable
from one sub-program to another sub-program which
needs to use the variable.
PARAMETER PASSING
PARAMETER PASSING
PARAMETER PASSING
PARAMETER PASSING (IN)
•In DisplayUserAge, we did not change the users age
so we only need to pass it IN.
•We call this pass by value.
•In Visual Basic we use ByVal to signify this.
•A copy of the original value is used by the sub-program.
•The copy is discarded once the sub-program is finished.
PARAMETER PASSING (OUT)
•In GetUserAge, we were obtaining (effectively
changing) the users age so we needed to pass it OUT.
•We call this pass by reference.
•In Visual Basic we use ByRef to signify this, we pass in a
reference to the value. This means the calling function
(MainProgram in this example) gets the new value back
out.
ACTUAL AND FORMAL PARAMETERS
•Actual parameter is the parameter that is passed (by
value or by reference) to the sub-program from another
part of the program.
•Formal parameter is the parameter in the sub-program
definition.
ACTUAL AND FORMAL PARAMETERS
Actual parameter
Formal parameter
PASSING PARAMETERS - EXERCISE
•You are now going to take a piece of code which uses
global variables and convert it to use local variables and
parameter passing.
•Your teacher will give you the instruction sheet.