2.2 Programming Fundamentals Flashcards
Operators
Special characters that perform specific functions
Common arithmetic operators
Addition
Subtraction
Multiplication
Division
Exponentiation
Quotient
Remainder
Exponential Operator
**
to the power of e.g. X**3
= X cubed
Quotient Operator
DIV
gives whole number product of division
e.g. 20 DIV 3 = 6
Remainder operator
MOD
gives remainder product of division
e.g. 20 MOD 3 = 2
What rule do computers follow?
BODMAS
Assignment Operator
’=’
assigns values to variables
Comparison Operator
Compare left hand expression to right hand expression
Common Boolean Operators
AND
OR
NOT
What does each boolean operator have?
Logic Gate
Logic Gate
Receive binary data, apply a Boolean operation, output a binary result
Has a truth table -> shows the possible input combinations and corresponding outputs
AND gate
Take 2 inputs, give 1 output
If both inputs are 1, the output is 1
If not, output is always 0
OR gate
2 inputs, give 1 output
If at least one of the inputs is 1, output is 1
If not, input is 0
NOT gate
1 input, 1 output
Output value is opposite to input value
5 main data types
Integer
Real
Boolean
Character
String
Using the correct data type makes code more efficient + robust
Data type: Integer
Whole number
‘int’
Data type: Real
Decimals
‘real’
Data type: Boolean
Can take one of two values: e.g. true or false
‘bool’
Data type: Character
A single letter, character, symbol
‘char’
Data type: String
To represent text
‘string’
Size of integer
2 bytes
Size of real
4 bytes
Size of boolean
1 byte
Size of character
1 byte
Size of string
1 byte per character
Casting
Functions to manually convert between data types
e.g. int(), real(), bool(), str()
Basic String Manipulation
Allows strings to be sliced (extract a single character), joined or modified
Basic String Manipulation actions
- .length - count no. of characters
- .substring(x,y) - extract character from middle of x and y
- .left(x) - extract x no. of characters from the left
- .right(x) - extract x no. of characters from right
- .upper - convert string to uppercase
- .lower - convert string to lowercase
Basic File Handling Operations
- open() - open file into variable
- read - read line from data of contents
- write - write line at the end of file
- .close - close file
Use of records to store data
Record = data structure which allows multiple data items to be stored.
Difference to arrays: different data types can be stored
Use fieldnames to identify each item
What is SQL?
Structured Query Language - used to access data on a database
Commands in SQL
SELECT * FROM (table) WHERE
Uses of 1D and 2D arrays
1D - To store multiple terms under 1 identifier
2D - store multiple terms of data using 2 identifiers
What is an array?
A data structure that can store a collection of data values all under one name
Creating a 1D array
e.g.
array rowers [3]
rower [0] = “sjsfn”
rower [1] = “ergg”
rower [2] = “tngm”
Retrieving element from 1D array
print (rowers[0])
Changing element in 1D array
rowers[0] = “Tamal”
Random Number Generation
random (x,y)
random number between x and y
Examples of sub programs
- Functions
- Procedures
Sub programs: Procedures
set of instructions stored under 1 name
Sub programs: Functions
Same as procedures, but return a value
Take atleast 1 parameter
Features of sub programs
- Parameters
- Arguments
Features of sub programs: Parameters
Special variables used to pass values into a sub program
Features of sub programs: Arguments
Actual values that the parameters take when sub program is called
What to do when calling a function
Assign to a variable where the returned value will be stored
What can variables be?
- Local
- Globa;
Local Variables
Used within the structure they’re declared in
Global Variables
Can be used any time after their declaration
Advantages of local variables
Won’t affect and are not affected by anything outside the sub program
Disadvantage of global variables
Can lose track of its value
IF statements
Allows you to check if a condition is true or false, and carry out a different actions depending on the outcome
Structure of IF statements
if
elseif
else
endif
Switch Statements
same as if statements, but check if a variable has a certain value
Structure of SWITCH statements
switch
case
case
case
endswitch
Disadvantage of SWITCH statements
They only check for the value of 1 variable
FOR loops
Repeat the code inside of them for a fixed number of times - depends on initial value, end value, step count
for k = 1 to 100 step 3
Types of Loops
FOR
DO UNTIL
WHILE
DO UNTIL Loop
Controlled by condition at end of loop - loops until condition is true
Structure of DO UNTIL Loop
do
until
WHILE Loop
Controlled by condition at start of loop - keep going until the condition is not true
Structure of While Loop
while
endwhile