A value stored in memory that can change while the program is running.
Constant
A value that does not change while the program is running ,and is assigned when the program is designed.
Assignment
Giving a variable or a constant a value
Casting
Converting a variable from one data type to another.Eg integer to string.A variable can be a real/float, character, integer, String,Boolean
Input
A value is read from an input device eg keyboard
Output
Data generated by the computer and displayed to the user
Advantages of constants
They make a program easier to read as 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
Inputs form the keyboard are always characters. Multiple characters are called a string. However to perform a calculation the arithmetic logic unit must use numbers. Therefore a string has to be "cast" to a number. Makes sense as the character 1 is stored as the binary number 00110001(using the ASCII character set).The number 1 that the arithmetic logic unit needs for calculation is stored as the binary number 0000001.
Integers to real
Integers require less bits of memory than decimal point numbers. Integers would make the 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.
Integer
Whole number
Positive or Negative
eg 6
use=total+score
Never used for telephone numbers only when arithmetic on the data is required
Real/float
Numbers with a decimal point
eg 6.5
use:cost=total+vat
Character
Single alphanumeric
Use for menu choices
eg "1" or "a"
Use: Choice=input("enteryourchoice")
String
Multiple alphanumeric characters
Use for words and telephone numbers
eg"Craig"
Forename="Craig"
Sequence
Sequence is executing one instruction after another
Selection(Branching)
Selection is a program branching depending on a condition
Selection(branching) aware for exam
Some programming languages support SWITCH/SELECT...CASE statements.Here a program can branch in more than directions depending on the value.
Iteration(looping)
Repeating sections of code.FOR loops (known as counter controlled loops)are used when the number of iterations needed is known ahead of the iteration executing.
Iteration(looping) different loops
WHILE loops(Condition controlled loops) are used when the number of iterations is not known because the variable used to determine when the iteration ends is changing within the iteration itself.
Do..UNTIL loops are alternative to WHILE where the code executes at leat once before the condition is checked
Common comparison operators
Common arithmetic operators
Boolean operators
Or,And,Not
example of Boolean Not operator
end_of_file=False
while not end_of_file.
The same as while end_of_file==false:
Use an umbrella if it is raining but NOT cold.
example of Boolean AND operator
end_of_file=False
found=False
while not end_of_file and not found
Wear a coat if it's raining and cold
example of Boolean OR operator
if x==2 or x==4
while choice<1 or choice>3
Catch a bus if it's raining or cold.
Create an infinite loop
While loop
What can variables and constants hold
integer, Real/Float,Character,String,Boolean
Importance of casting
Necessary so that sub-problems receive data in expected format. Casting allows numbers to be manipulated as strings. Casting allows inputs that are always strings to become numbers.
x=str(X) casts the variable x to a string
x=int(x) casts the variable x to an integer
To get the length of a string
Returns the length of a string eg length=string.length
Converting Cases
uppercase=string.upper
lowercase=string.lower
To extract a substring
Returns part of a string ,starting at the character of the first parameter and counting up by the number in the second parameter .
String name.substring(starting position, number of characters)
Chars=string.substring(3,1) hello returns "l"
To extract from the left or right of a string
Returns to the left most or right most characters from a string where the parameter indicates how many to return.
Eg left = string.left(8)string ComputerScience returnComputer
Eg right = string.right(7)string ComputerScience returnScience
Concatenation
+ joins separate string values together
eg print(stringA + stringB)
ASCII Conversion
Returns the ASCII value of a character .Returns a character from its ASCII number.ASC(),CHR()
eg ASC(character) ascii=ASC("A") CHR=(ascii number) char=CHR(65) eg Ascii would be 65 eg char would be "A"
Example of ASCII conversion
someText="computer science"
print(someText.length) 16
print((someText.substring(3,3)).upper)PUT
Get the length of the string
len(string)
Cases of a string
U string=string.upper()
l string=string.lower()
Substring of a string
string[x:i]
Stages to write data to a file
Open the file for creating/overwriting or appending to a file
Write the data to a file
Close the file
Stages to read data from a file
Open the file for reading data
Assign a boolean variable to "false" to indicate that the end of the file is not reached.While the end of the file is false and the search item is not found:read the data from the file.If the data matches what is being searched for ,assign the data to variables or output.Check if the end of the file is reached and assign the boolean variable to "true".Close the file