2.2.1 Programming Techniques Flashcards
1
Q
Sequence
A
Code is executed line-by-line, from top to bottom
2
Q
Selection
A
A certain block of code is run if a specific condition is met, using IF
statements. This is also known as ‘branching’.
3
Q
Iteration
A
Can be either:
Count-controlled
- Block of code executed a certain number of times
Condition-controlled
- Block of code is executed while a condition is met
Uses FOR, WHILE or REPEAT UNTIL loops
4
Q
Recursion
A
- Programming construct in which a subroutine calls itself during its execution
- Continues until a stopping condition is met
- produces the same result as iteration, but is more suited to certain problems
5
Q
Recursion - Advantages
A
- Can be represented in fewer lines of code (for some problems), making them less prone to errors
- Easier to express some functions recursively eg. Factorial
6
Q
Recursion - Disadvantages
A
- Inefficient use of memory
- Risk of stack overflow if memory runs out
- Difficult to trace (especially with more and more function calls)
7
Q
Variables
A
- Variables can be defined with either global or local scope
- Scope is the section of code in which the variable can be accessed
- A local variable within a subroutine takes precedence over a global variable with the same name
8
Q
Local Variables
A
- Can only be accessed within the subroutine in which they were defined
- Multiple local variables with the same name can exist in different subroutines
- Are deleted once subroutine ends
- Using local variables ensures subroutines are self-contained - good
programming practice
9
Q
Global Variables
A
- Can be accessed across the whole program
- Useful for values that need to be used by multiple parts of the program
- Danger of being unintentionally edited and overwritten
- Not deleted until program terminates, so require more memory
10
Q
Modular Programming
A
- A programming technique used to split large, complex programs into smaller, self-contained modules.
- Modularity is essential to making a problem easier to understand and approach.
11
Q
Benefits of a Modular Approach
A
- Easier to divide tasks between a team and manage projects
- Simplifies the process of testing and maintenance, as each component can be dealt with individually
- Improves reusability of components - once a module has been tested, it can be reused with confidence
12
Q
Top-Down Approach
A
- Technique used to modularise programs
- Problem is broken down into sub-problems, until each is represented as an individual, self-contained module which performs a certain task
- Modules form blocks of code called subroutines
13
Q
Functions vs Procedures
A
- Both named blocks of code that perform a specific task
- Procedures do not return a value
- Functions must always return a single value (some languages, like Python, allow functions to return multiple values using tuples)
- Parameters can be passed into a subroutine either by value or by reference
14
Q
Passing By Value
A
- A copy of the value is passed to the subroutine and discarded at the end
- Its value outside of the subroutine remains unaffected
15
Q
Passing By Reference
A
- Address of parameter is given to the subroutine
- Value of the parameter will be updated at the given address