2.2.1 Programming Techniques Flashcards
1
Q
Programming constructs
A
- a programming construct determine the order in which lines of code are executed
- 3 main programming constructs
- selection
- iteration
- sequencing
2
Q
Sequence
A
- code is executed line by line in sequence from top to bottom
3
Q
Iteration
A
- repeating a line or block of code using a loop
- can either be count controlled or condition controlled
4
Q
Count controlled
A
- (for loop)
- is used when the number of iterations is known before execution and the block of code is executed a certain number of times
5
Q
Condition controlled
A
- used when the number of iterations is not known so the block of code is only executed while a condition is met
- while loop, do until loop
6
Q
Branching
A
- the particular block of code is run if a specific condition is met
- eg. If else statements
7
Q
Recursion
A
- is when a subroutine/function calls itself from within its own subroutine/function [1]
- the function continues to call itself until a base case/stopping condition is met [1]
8
Q
Importance of base case
A
- without a base case, a recursive function may call itself indefinitely resulting in a stack overflow
- the base case should be reachable within a finite number of times
- for input values other than the stopping condition, the function must call itself
9
Q
Recursion benefits
A
- quicker to write/ requires less lines of code [1] as some functions are naturally recursive
- can reduce the size of a problem with each call [1]
- recursion is suited to certain problems e.g problems involving trees or factorials [1]
10
Q
Recursion drawbacks
A
- more difficult to trace as each frame on the stack has its own set of variables [1]
- requires more memory compared to the equivalent iterative algorithm in order to maintain the stack of calls [1]
- risk of stack overflow if memory runs out e.g due to too many calls [1]
11
Q
Scope
A
- the section of code in which a variable can be accessed
- a local variable within a subroutine takes precedence over a global variable with the same name
12
Q
Local variables
A
- can only be accessed with the subroutine in which they were defined [1]
- multiple local variables with the same name can exist in different subroutines [1]/ same variable name be used in other modules without causing errors/overwriting
- are deleted once the subroutine ends [1]
- can overwrite global variables with the same name [1]
13
Q
Global variables
A
- can be accessed across the whole program [1] (so no need to pass as a parameter or redeclare
- is used when a value needs to be accessible from various parts of a program [1]
- value remains the same irrespective of where it is accessed [1]
- e.g values of constants like pi or VAT rate [1]
- not deleted until the program terminates so requires more memory
14
Q
Global variables drawbacks
A
- make it difficult to integrate modules [1]
- increase the complexity of a program [1]
- they may cause conflicts with names in other modules [1]
- may be modified unintentionally when the program is complex [1]
15
Q
Modular programming
A
- programs are split into separate self contained modules [1]
- which can be written/tested individually [1]
- modules can be subdivided further into smaller modules [1]