The use of Variables, Constants, Inputs, Outputs & Assignments Flashcards
Define variable
A variable is a value stored in memory that can change while the program is running
Define constant
A constant is a value that does not change while the program is running, and is assigned when the program is designed
Define assignment
Assignment - Giving a variable or constant a value
Define casting
Casting - Converting a variable from one data type to another
Give an example of casting
For example, integer to string.
What data types can a variable be
A variable can be an integer, character, string, real (float) or Boolean.
Define input
Input - A value is read from an input device
e.g. keyboard
Define output
Ouput - Data generated by the computer and displayed to the user.
State the advantages of constants over variables
Constants make a program easier to read as they are usually declared and assigned at the top of the program
Constants can easily be changed by the programmer in one place in a program .
Instead of changing every instance of a value throughout a program.
This leads to less chance of errors
The complier can optimise the code.
This makes a program run more quickly if constants are used instead of variables.
Why is casting needed
Casting is needed because:
Inputs from the keyboard are always characters. Multiple characters are called a string.
However to perform an addition, the Arithmetic Logic Unit (ALU) must use numbers.
Therefore a string has to be “cast” to a number
Integers require less bits of memory than numbers with a decimal part (real numbers)
Therefore it makes sense to use integers where we can to make a program more memory efficient.
However, it may be necessary to cast an integer to a real in a program.
Some commands also require data in a particular data type
What is an integer
An integer is a whole number
(positive or negative)
When would you use and not use integers
Use: total = total + score
Never use for telephone numbers
Use only when arithmetic on the data is required
Give an example when an integer would be used in a piece of code
Integer
Use: total = total + score
What is a real
A real is a number with a decimal part
What is a real also called
Real also called a float (floating point number