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