Programming Flashcards
Why is it necessary to have a variety of programming languages?
- different languages suit different jobs
- developers prefer certain languages
computer program
a series of instructions that are executed one after another.
follow the pattern: INPUT -> PROCESS -> OUTPUT
3 programming constructs
sequence, selection, iteration
Sequence
do one statement after another in the correct order
Selection
do a set of statements based on conditions allow your code to make choices- otherwise known as branching
iteration
do a set of statements again and again (looping)
count controlled loops
repeat a set number of statements a fixed number of times
condition controlled time
repeat a set number of statements until a condition is met
Variable
a name refer to a particular memory location that is used to store data. The value of the data held in that memory location is not known when the program is written and can change while the program is running
constant
name used to refer to a fixed value. The value is set when the code is written and cannot change while the program is running
procedure
a self-contained set of commands that can be called from different parts of the program. returns 0 or many values.
function
a sub-routine that make take one or more parameters and ALWAYS returns a value
uses of functions and procedures
breaking a problem into manageable sections
preventing duplicating sections of code when it is needed more than once in a program
why is manageable code important
easy to read, debug and easy to maintain
\
integer division
AND
if both inputs are true the output is true else the output is false
OR
if both inputs are true or one input is true the the output is true else the output is false
NOT
reverses the outcome of the expression
assignment operator
single equals, sets the value on the right equal to the value on the left
procedural language
programming languages where the user gives step by step instructions of what to do
assembly language
a low level language represented by mnenomics which represents the machine code. one-to-one relationship with machine code.
programming paradigm
a way to classify programming languages based on their features.
3 programming paradigms
`procedural
Object Orientated
Assembly
why can assembly code for one processor not work for another processor
each type of processor has its own unique instruction set
Load instruction
LDA
store instruction
STA
input instruction
INP
Output instruction
OUT
end instruction
HLT
data storage instruction
DAT
add instruction
ADD
subtract instruction
SUB
branch always instruction
BRA
branch if zero or positive instruction
BRP
branch if zero instruction
BRZ
procedural language examples
Pascal, C, Basic and Python
Object Orientated languages examples
C++ and Java
Object Orientated language
coders develop a solution which consists of objects that have been modelled on the real world.
Class
a template to set out a define what attributes an object of a certain type should have
three main class sections:
class name
attributes
methods
instantiation
the process of creating an object from a class template
constructor method
signified by the key word new, initiated instantiation
inheritance
when a class takes on the methods and attributes of a parent class. The inheriting class may override or add new methods/attributes.
Superclass
the original class and its code
Subclass
the new class which inherited from the superclass
Over-riding
when the method in the subclass shares the same method as further up in the object tree. the over-ridden method takes precedence and replaces the method further up the object tree
Encapsulation
the protection of attributes and methods of an object so that they can’t be accessed or altered by other objects. (keyword - private)
polymorphism
when something appears in multiple forms and the program makes sure the correct meaning of a function is run
assembly -> machine relationship
one-to-one
high level -> machine relationship
many-to-one
four ways of addressing memory in a computer
immediate
direct
indirect
indexed
immediate addressing
when the value in the operand is the actual value to be used. this means the memory does not need to be searched
direct addressing
where the operand contains the address of the value to be used.
indirect addressing
where the operand contains the address which contains the address where the value can be found.
useful because it means that the larger addresses in memory can be used to store
indexed addressing
The value given is added to the valued stored in the index register to give the memory location.
Benefits of assembly code (over high level)
- efficient as the code is more specifc
- direct control of hardware
- good if no compilers/interpreters installed
- good if limited memory
Benefits of high level code (over assembly)
- optimisers may make code more efficient
- intuitive and easier to write
- can be recompiled for different architechtures
- less code is written (shorter programs)
for loop pseudocode
for i=0 to 7
print(“Hello”)
next i
while loop pesudocode
while condition=
code
endwhile
do until loop pseudocode
do
code
until
condition
MOD
gives the remainder of a division
DIV
integer division (rounds down)
if/else Pseudocode
if condition then code elseif condition then code else code endif
switch/case pseudocode
switch condition: case “A”: code case “B”: code default: code endswitch
get the substring
stringname.subString(startingPosition, numberOfCharacters)
by default how are parameters passed?
by value
reading from a file
myFile = openRead(“sample.txt”)
x = myFile.readLine()
myFile.close()
writing to a file
myFile = openWrite(“sample.txt”)
myFile.writeLine(“Hello World”)
myFile.close()
procedure OO pseudocode
public procedure
code
endprocedure
Inheritance OO pseudocode
class Dog inherits Pet code endclass
create an instance of a class OO
objectName = new className(parameters)
Attribute
variables contained within and associated to an object
Method
a subroutine associated with an object
Object
Instance of a class
Characteristics of Procedural
instructions, sequences, selections, interaction, procedures and functions
Advantages of Using encapsulation
- reduces chance of error
- consistent changes
- prevents accidental changes
using a two dimensional array
[1,3]
the first number is the column the second number is the row this would extract the number in the 1st column and the 3rd row
//
comment
.length()
find the length of the variable in front of the dot
auto-complete
feature of an IDE
can view identifiers and help avoid spelling mistakes/ typos
syntax highlighting
feature of an IDE
identify features quickly, makes it easier to check code is correct
stepping
feature of an IDE
run one line at a time and check result
breakpoints
feature of an IDE
stop the code at a set point to check the value of variables
variable watch window
feature of an IDE
checks values of variables and how they change during execution
error diagnostics
feature of an IDE
locate and report errors
why would something be passed by reference and not by value
- changes the value in the variable passed
- passes the address/location
- the change won’t be lost when the procedure ends
write a constructor method
PUBLIC PROCEDURE NEW(parameters)
SET ATTRIBUTES
END PROCEDURE
done inside the class your making an instance of
create an instance of a class
variable = new class(values for attributes)
passing by value
sends the actual data, if changes are made only the local copy is amended
passing by reference
send the address/location of the variables, if a change is made the original is also changed.
benefits of passing by reference
- existing memory space is used, no extra memory is required
- removes inconsistencies when passing to other procedures
benefits of passing by value
- doesn’t change the original variable
- good if you don’t want the original variable by edited
decomposition
splits problems into sub problems, splits these problems further until each problem can be solved
advantages of decomposition
- increase speed of development
- assign areas to specialatieis
- re-use modules
disadvantages of decomposition
- need to ensure sub programs interact correctly
- can introduce errors
advanatages of abstraction
- reduces processing/memory requirements
- increases response speed of programs
caching
data that has been used is stored in cache in case it is needed again allows for faster access for future use
benefits of concurrent processing
- saves time so long as processes are independent
drawbacks of concurrent processing
- tricky to test
- tricky to code
- won’t neccessarily be twice as fast
- takes up more memory
parallel computing
When more than one processor is executing separate instructions at the same time
concurrent processing
When more than one process is running from a program at once with each process in turn being given a slice of processor time/ running on a separate core
Default setting of methods and attributes
public
calling a method
object.method(parameters)
calling a superclass method
super.methodName(parameters)
for constructor: super.New()
Inheritance pseudocode
Class dog inherits pet
set attributes
methods
endclass
Uses of superclass methods
subclass methods override the superclass method to support the greater specialisation defined by the subclass. It can be useful to call the superclass version before executing the specialised subclass method.
Calling the superclass method from an inherited class
public procedure new(parameters for class)
super.new(name)
set attributes
end procedure