2 - Practical Problem Solving and Programming / Variables & Data Types
|
|
Variables & Data Types
As programs start to become more complex it is vital that they are able to remember information. Without this ability all software would be limited to extremely simple functions. Variables and Constants provide the basic storage ability that allow more complex programs to exist.
Variables & Constants
Variables and Constants are areas of memory that have been reserved to store data. This data can be called upon and used by the program at any point after they have been created.
Using Variables
Declaring Variables
It is good practice to declare (set up) your variables at the start of a program. The reason for this is that each time you declare a variable, a part of the computers memory is reserved for it. Declaring these variables at the start allows the computer to calculate whether or not it actually has enough memory to run the program.
The name of a variable can also be refereed to as its identifier. A variable can be given any name, however it is good practice to name it something that relates to the information that it will store.
It is acceptable to use single letter names e.g. i = 0 or x =1 when using variables in conjunction with Arrays (for index) or loops (for counting).
When a value is placed into a variable it is known as Assignment.
It is good practice to declare (set up) your variables at the start of a program. The reason for this is that each time you declare a variable, a part of the computers memory is reserved for it. Declaring these variables at the start allows the computer to calculate whether or not it actually has enough memory to run the program.
The name of a variable can also be refereed to as its identifier. A variable can be given any name, however it is good practice to name it something that relates to the information that it will store.
It is acceptable to use single letter names e.g. i = 0 or x =1 when using variables in conjunction with Arrays (for index) or loops (for counting).
When a value is placed into a variable it is known as Assignment.
Data Types
Variables and Constants are responsible for storing data. This data needs to be stored differently and will require a different amount of memory depending on what form it is in. e.g."Hello" is different to 7. These two types of data need to be treated differently. 7 is a number and should be able to be used in mathematical equations where as "Hello" should not be involved in Maths. In order to manage this, each piece of data is defined as a specific data type.
The data types available for use are:
The data types available for use are:
String
The String data type is used for data that can be used to store a number of letters, digits and symbols. The data can be a combination of these three items e.g. "345ISS@", or it can be just one type e.g "Hello World".
When assigning data to a string it should be placed within quotes e.g. name = "Dave"
Strings are printable, Mathematical operations cannot be performed on strings even if they only contain numbers.
e.g. if you have the following code:
x = "7"
y = "3"
OUTPUT x + y
The result that is output for the user would be "73" NOT 10. This is known as concatenation, 2 or more strings can be joined together using the + symbol.
When assigning data to a string it should be placed within quotes e.g. name = "Dave"
Strings are printable, Mathematical operations cannot be performed on strings even if they only contain numbers.
e.g. if you have the following code:
x = "7"
y = "3"
OUTPUT x + y
The result that is output for the user would be "73" NOT 10. This is known as concatenation, 2 or more strings can be joined together using the + symbol.
Integer
An Integer can be used to store a positive whole number (No decimals numbers)
Integers will allow you to perform mathematical operators e.g. if you have the following code
x = "7"
y = "3"
OUTPUT x + y
The result that is output to the user would be 10
Integers will allow you to perform mathematical operators e.g. if you have the following code
x = "7"
y = "3"
OUTPUT x + y
The result that is output to the user would be 10
Real
The Real data type will allow you to store decimal numbers (Positive or negative) e.g. Cost = 5.50
Real data can be used in mathematical operations
Real data can be used in mathematical operations
Char
The Char data type can be used to store one single character.
e.g. Gender = "M"
e.g. Gender = "M"
Boolean
The Boolean data type can have only two possible values, these are TRUE or FALSE.
E.g. Accepted = true
E.g. Accepted = true
Vertical Divider