Topic 2 - Programming Flashcards
What’s Declaration
Before variables can be used, they must be declared.
Declaring a variable means telling the computer:
What the identifier (name) should be.
What type of data will be stored.
What’s assignment
Changing the value stored inside a variable is called assignment.
The value assigned to a variable must match the data type of the variable.
Why do some languages not need to declare the variable
Some languages don’t need you to declare variables before you use them:
These languages automatically declare the variable before the first assignment.
What’s input
Input is collecting data, usually by the keyboard.
This is written in pseudocode as:
OUTPUT “Please enter [something]…”
variable = USERINPUT
What’s output
Output is putting data to the screen, usually as text.
This is written in pseudocode as:
OUTPUT “Text goes here”
What’s a string
Text data.
Example:
“Hello, world!”.
What’s a character
Character
A single letter of text data.
Example:
‘a’.
What’s a Boolean
A True or False value.
Examples:
True.
False.
What’s a real
Fractional numbers.
Examples:
0.42, 1.00.
What’s an integer
Whole numbers.
Examples:
12, 0, -142.
What are the reasons for casting
One of the most common reasons for casting is output.
Output must be formatted as a string, and so we may need to convert a certain piece of data to a string.
All input also comes as a string, and must then be converted to other data types.
What are casting functions
Casting to a string can be done using the str function. E.g:
str(3) gives “3”.
Casting to an integer can be done using the int function. E.g:
int(3.4) gives 3.
Casting to a real can be done using the real function. E.g:
real(“3.4”) gives 3.4.
What are the basic operators
Addition is done using a + sign. Subtraction is done using a - sign. Division is done using a / sign. Multiplication is done using a * sign. Exponentiation is done using a ^ sign.
What’s a modulo operators
The modulo operator gives the remainder of the division of two numbers.
For example:
5 MOD 2 would be equal to 1.
What’s integer division
The integer division operator returns the quotient (whole part) of a division.
For example:
5 DIV 2 would give 2.
How do we test for comparisons
We can compare two numbers using the less than and greater than signs. For example:
2 < 3 evaluates to True.
2 > 3 evaluates to False.
We can test for less than or equal, or greater than or equal like this:
2 ≤ 3 evaluates to True.
3 ≥ 3 evaluates to True.
How do you test for equality
We can test if two values are equal using the equality operator. For example:
2 = 2 would evaluate to True.
We can test if two values are not equal using the not-equal-to operator:
2 ≠ 2 would evaluate to False.
What do ORs do
OR evaluates to True if any of the two operands are True: True OR True = True. True OR False = True. False OR True = True. False OR False = False.
What do NOTs do
NOT negates a logical value.
NOT True = False.
NOT False = True
What do ANDs
AND evaluates to True if and only if both operands are True. True AND True = True. True AND False = False. False AND True = False. False AND False = False.
What happens in a sequence algorithm
In a sequence algorithm, the computer follows a series of steps that are executed in the same order every time it runs.
This is often not very useful for longer codes because we cannot use any selection or iteration structures
What happens in a flow chart
The flow chart for a sequence program will have the blocks one by one.
One arrow will connect each block to the next block.
There will be no diamond shapes, so the diagram will be mainly:
Ovals.
Rectangles.
Parallelograms.
What’s selection
Selection allows us to execute a section of code depending on whether a condition is met or not
What’s Does an if statement do
An if-statement is an easy way of checking if a condition is true.
What’s a switch case statement
A switch-case statement checks the value of a single variable
What’s used to show selection in a flow diagram
The diamond symbol is used to show selection in a flow diagram.
What’s definite iteration
A block of code will repeat a known number of times.
What does a flow diagram use for iteration
A flow diagram for iteration uses the diamond shape
What’s indefinite iteration
A block of code will repeat while a specified condition is true
What are the advantages of subroutines
Easier to read:
There are fewer long blocks of code to understand.
More efficient:
Blocks of code can be written once and reused many times.
More reliable:
Each subroutine can be individually tested to make sure it works.
What are parameters and arguments
Parameters are special variables used to pass values into a subroutine.
Arguments are the actual values that are passed in.
What are the types of subroutines
A function is a subroutine which returns a value.
A procedure is a subroutine which does not return a value
What’s a local scope
A variable defined within a subroutine will have local scope.
This means that they can only be accessed within that subroutine
What’s a global scope
A variable can be made global by adding the ‘global’ keyword in front of it when it is first used.
Global variables may be accessed by any part of the whole program
What are arrays
An array is a data structure that stores a fixed number of values under a single identifier.
The values must be of the same type.
What are array elements
Each piece of data inside an array is called an element.
Each element has an index, which is its position within the array.
We start counting the indices from 0.
What are arrays in pseudo code
An array of ten items can be created using the following syntax: array name_of_array[10] Values can be assigned as follows: name_of_array[0] = "Hello" Values can be accessed as follows: OUTPUT name_of_array[0]
What’s concatenation
Concatenation is joining two strings together.
This can be done with the + sign. E.g:
“Hello “ + “World” = “Hello World”.
How do you asses a character
Remember, strings are just lists. So we can access a specific character in the exact same way as we access a list element:
string[3] would get the fourth character
How do you know the length of a string
The length of a string is given by the len function:
len(“Hello”) would give 5.
What’s a substring
A substring is a part of a longer string.
In pseudocode, we use the function:
substring(startPos, endPos, string)
This takes a substring starting at the ‘startPos’th character, and ending at the ‘endPos’th character.
What are lists
Lists store multiple elements under the same identifier.
There can be a variable number of elements - they never get full
What are list is in pseudocode
A list can be declared as follows:
pupils = []
Values can be added to the end of the list as follows:
pupils.append(“Alice”)
Values can be accessed as follows:
print(“The first student is “ + pupils[0])
What’s a 2D list
A 2D list is a data structure where lists are filled with other lists.
This can be used to represent a 2D surface, or records.
How do you access elements lists
To access an individual list, we can access the list as usual:
list = my2DList[4]
To access an individual element, we use this syntax:
list[5][0]
This would get the first element of the sixth list.
What are the uses of 2D lists
2D lists are often used to represent a 2D surface, for example a chess board:
Each element of the 2D list could contain the chess piece in that location.
2D lists can be used to represent a database:
Each inner list can store a record of the database.
The outer list can store each record.
What are text files
Text files consist of string data that can be read and processed by many applications
What are files
Files allow us to store data so that it persists even when our program is not running.
This allows programs to ‘carry on where they left off’ when they are executed.
What are binary files
Binary files consist of binary data that is read and processed by specific applications
Why do we need to be carful when we access files
We must be careful with the way we access files so that we do not accidentally corrupt them
How do we open files
First we open the file for use, so that the operating system knows that we are using it.
We can do this using:
openRead(“filename.txt”)
openWrite(“filename.txt”)
How do we assign the variable
The file must be assigned to a variable so that we can keep refering to it.
This can be done as follows:
myFile = openRead(“filename.txt”)
myFile = openWrite(“filename.txt”)
How do we read and write files
We can now read or write to the file using the following commands:
myFile.readLine() will return the next line of a file as a string.
myFile.writeLine() will write a line to the file.
myFile.endOfFile() will return True if we reach the end of the file
Why do we close files and how
Finally, we must close the file so that it is free for another program to access.
We can do this using:
myFile.close()
What does structured data refer to
any data that resides in a fixed field within a record or file so that it can be easily entered, stored, queried, and analysed
What are records
Records are made up of fields.
For example, a person could be represented as a record where the fields include their name, age, and address
What are Comma separated values (CSV) and what do they do
One method of storing a record is using comma separated values.
Each record is put on its own line.
Each field is separated by a special character such as a comma
What are the pros and cons of records
Pros: Very simple, text-based format. Can be read by many applications. Easy to use in programs. Cons: Inefficient for large datasets. Can only store text data. No built-in means of sorting or searching