221-programming-techniques Flashcards
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
- simplifies program, fewer lines of code, less error prone
- more flexible, can just change loop value to change number of iterations
nesting
putting one statement inside of another, achieved by either iteration or selection/both
recursion
- a function that calls itself within itself
- for any input value other than stopping condition, it should call itself
- the stopping condition should be finitely reachable or else stack overflow (indefinite calls)
- each call creates a stack frame (new instance of a function with its own copy of local variables and parameters and return address - code point)
- frames placed on the stack
- then stopping, they pop off until stack emptied, return results are cascaded down to caller until initial caller
recursion pros and cons
- more quick to read, easier to represent/ less lines of code as some functions/problems are naturally recursive
- suited to certain problems e.g tree traversal and some sorts
- can reduce the size of the problem each call e.g divide and conquer
- deep -> runs out of stack space if too many calls and crash occurs (overflow)
- more difficult to trace flow and debug, each function on the stack has its own set of variables
- less intuitive for those unfamilar
- inefficient use of memory
- can be slower as overhead stack maintenance
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
- more memory efficient, requires less
- no stack overflow
- easier to understand, debug read, and trace as straightforward
- recursive declares new variables/stack push, iterative uses the same variables
- for problems that can be naturally expressed iteratively
- more lines of code, potentially harder to understand when implemented to recursive problems (representation)
tail recursion
- keeps an accumulated total of the value we are calculating
- usually using a accumulator parameter
- reduces computation, no additional calculations after recursive call returns as final recursive return value is the final return value
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
breakpoints
allows developer to define certain points in program to pause execution on condition/line
helps pinpoint
and identify logic errors
stepping
step through/execute code line by one at a time to locate error and monitor effect of each line
Variable checks
shows the values of the variables currently stored in memory whilst stepping through the code, observe how contents change real time
runtime environment
infrastructure supports execution and running of code, enable developer to
check for runtime errors and test program without fully compiling it
auto documentation
analyse source code
tracks declared variables, modules, comments
- produces documentation to aid in maintenance, debugging, support
syntax highlighting
easier to read/identify syntax errors as coding
error diagnostics
diagnostics displays full report on syntax and runtime errors during and location during compilation, sometimes suggests fixes
syntax completion
as you type, suggests/correct code
quicker to type, less errors
code editor
area to write and edit source code
often supports additions like autocomplete, autoindent, syntax highlighting
operator
character that represents an action e.g +