Computer program
series of statements (instructions) that are executed (run) one after the other
- accept input, process data, produce output
Programming constructs
controls the flow of the program
iteration sequence, selection
sequence
the order of which code statements are executed
from top to bottom until the end of the program is reached/another construct
- important as carrying out instructions in wrong order lead to incorrect performing
selection
changes program flow based on outcome og logical decision e.g if statements
allows program to branch as
section of code is only run if condition is met
iteration
loops used to repeat sections of code for execution
count controlled e.g for loop - for a certain amount of time, required iterations is known ahead of time
condition controlled e.g while loop - until condition is met, when number of iterations is not known
nesting
putting one statement inside of another, achieved by either iteration or selection/both
recursion
recursion pros and cons
call stack
stores the local variables, parameters, and the return address when a function is called
when functions complete execution, they are popped off
iteration pros and cons
tail recursion
variable
labelled memory location (identifier) that stores value that can be changed during running time
constant
labelled memory location whose value remains fixed
does not change during program running
must be set when written
assignment
supplies a value to constant or variable, performed with a = symbol
scope
global or local
refers to the section of code for which variable is available in when declared
global variable
-can accessed/changed throughout entire program. useful for values need to be used by multiple parts
- declared global/ in main body at top, simple.
- good practice to avoid global variables e.g if 2 functions try to access global variable at same time, conflicts and cluttered namespace
- harder to test, debug, maintain, returning values much safer
-created when program starts, destroyed when end, more memory alloc
local variable
declared within function/procedure
can only be accessed within that block
- memory efficient, released from memory
- reusability: multiple same names can exist in different subroutines, unaffected
- self contained and encapsulated, no unintended outside changes
-if local variable has same name as global, local precedence
- code redundancy, same purpose/value across subs
modularity
functions and procedures
- goal each module carries out specific individual task
- split large complex programs into sub self contained smaller (breaks down and structures)
subroutines
section of code that performs a specific task, can be called whenever needed
given a unique name identifier
- problem shorter n easier to understand solve read debug, simplifies testing isolation, parallel team division time save, individually, reusability and existing libraries, avoid repetition, abstraction
procedure vs function
both performs specific task when called
procedure does not return any value, function does
both can have any many parameters. are subroutines
parameter vs argument
Parameters are variables initalised in the function’s definition for it to accept inputs. Specifies what function expects. Act as placeholder for the values (arguments) when called
Argument are the values passed into function/procedure filling the parameters
- by reference or value
- reference is address of original value, og value updated if any changes made
- value is copy of og value, treated as a local variable, new space for it and end discarded, og value outside not affected
string manipulation
stringname.length
conversion
stringname.upper()
stringname.lower()
ord(”A”) gives ASCII, can use ASC
chr(65) gives char
boolean
stringname.isupper()
stringname.islower()
file manipulation pseudocode
file=openWrite(“s.txt”)
file=openRead(“s.txt”)
file.writeLine(“Hello”)
file.close()
x =file.readLine()
while NOT file.endOfFile():
print(myFile.readLine()
IDE (integrated developer environment)
software/ program used to write code
provides tools and features to help developer write, test, debug
editors, error diagnostics, run time environment, translator
speed up development and productivity
suitable for beginners, simple
autocompletion/indentation, syntax highlighting, integrated debugger