2.2.6. Programming Techniques
No description yet. This curated deck is ready for daily study.
Card preview (18)
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.
Inheritance
subclasses inherit methods and attributes from the superclass. Using inheritance, you can reuse the code from a class and extend its attributes and methods.
Encapsulation
When attributes are made private. Public methods are needed to read/ amend the attribute’s value - used to hide values or the internal state of an object preventing direct access by unauthorised third parties - reduces human error
Object Orientated Techniques
Attempts to capture or group info and data and related functionality (code) into structured items known as objects.