2.2.6. Programming Techniques Flashcards
Sequence
the code is executed line by line, from top to bottom (sequentially)
Branching/ selection
BRA - always branch
BRP - branch if the value is positive (or zero)
BRZ - branch if the value is zero
A certain block of code is run if a specific condition is met, using IF statements
Iteration
A block of code is executed a certain number of times (FOR loop) or until a condition is met (WHILE loop)
What is Recursion
- Where a subroutine calls itself within its own subroutine until a (stopping) condition is met.
Advantage of Recursion
The code is represented in fewer lines of code
Disadvantages of Recursion
- inefficient use of memory. If the subroutine calls itself too many times, there is a risk of stack overflow which will cause the program to crash
- errors within recursion are difficult to trace
Global Variables
Can be accessed across the whole program
Global variable disadvantages
- can be unintentionally overwritten
- requires more memory as they aren’t deleted once the subroutine is completed.
Local variables
local variables have a limited scope which means that they can only be accessed within the subroutine in which they were defined in
Local variable advantages
- ensures subroutines are self-contained (good practice in coding)
- multiple local variables with the same name can exist in different subroutines (won’t overwrite each other)
Modularity (top-down programming)
Splitting large, complex problems into smaller self-contained modules
- makes it easier to divide tasks between a team as each component is self contained
Procedure and function similarity
Procedures and functions are both named blocks of code that perform a specific task
Procedure vs function
Procedures don’t have to return a value whereas a function must always return a value
IDEs
- A program which provides a set of tools to make it easier to write, develop and debug code
- Debugging tools have run-time detection of errors to show where they may have occurred
- Stepping which allows you to monitor the effect of each individual line of code by executing line by line
- Source code editor which provides features such as autocompletion of words, indentation, syntax highlighting and automatic brack completion
Class
A class is a template for an object and defines the state and behaviour of an object It is not an object itself. It is a template used to set or define what attributes and methods an object has. Once you have defined a class, you can easily reuse that class and create many objects of that class.