A Level Computing#BristolMet
Session ObjectivesU2#S10
MUST describe the difference between constants, local and global variables
SHOULD explain why constants are used and how variables are intialised
COULD create a program that concatenates string variables
Create a program which allows a user to input name, date of birth and outputs the result + star sign
A Level Computing#BristolMet
Key Words
Constant
ConcatenateInitialise
Local Variable Global Variable
Modularisation
A Level Computing#BristolMet
Constants and VariablesMany programs contain values that do not change whilst the program is running, for example Pi.
Novice programmers may use ‘literals’ in their code for these values i.e circumference = 3.14159 * diameter
Best Practice – it is better to declare the value of Pi as a constant first i.e
CONSTANT Pi = 3.14159VARIABLE diameter
Circumference = Pi * diameter
TASK: Write and test this program in JavascriptEXT: Write and test a program to calculate the area of a circle in Javacsript.
A Level Computing#BristolMet
Constants and VariablesFor the extension you will have had to declare a variable to store the value of the radius.Variables can be either Local or GlobalLocal variables are declared and used inside a subroutine or ‘module’ and can only be used in that section code.Global variables are declared at the beginning of the program and be accessed throughout the code, including any subroutines.
TASK: Although it is not modular Discuss what type of variable you have used in your Javascript programs
TASK 2: Write and answer Q1 a and b p.118 Hodder
A Level Computing#BristolMet
Initialising VariablesSome programs initialise when they are declared which means they are given a starting value:
Usually integers are set 0, Booleans set to FALSE and strings are set to empty.
Consider this algorithm for adding any 5 numbers and outputting the total
BEGINFor i = 1 to 5INPUT NumberTotal = Total + NumberNEXT iOUTPUT Total
END
This will only work if the value of Total is set to 0 to begin with. If the system does this automatically then it will work otherwise Total will need to be initialised by inserting the lineTotal = 0 at the declaration or assignment of the variableTASK: Attempt to create this program in Javascript and Python. Compare the 2 languages
A Level Computing#BristolMet
String Manipulation:ConcatenateMany programs need to deal with text therefore functions and operations are provided that manipulate strings.
Concatenate - this means to join 2 strings together to make one. The + operator is usually used for this operatione.g FullName = FirstName + Surname
FullName = FirstName +“ “ +Surname would be a better way to write this, why?
TASK: Design and create a programme in Javascript which allows a user to input their name and birth date and output the result in one line.EXT: Extend the program to also output the users star sign
TASK 2: Now attempt part c of Q1 p.118
A Level Computing#BristolMet
Key Words
Constant
ConcatenateInitialise
Local Variable Global Variable
Modularisation