2.2.1 programming techniques Flashcards
name 3 programming constructs
- sequence
-selection
-iteration
sequence
Code is executed line-by-line, from top to bottom
selection/branching
A certain block of code is run if a specific condition is met, using IF
statements. This is also known as ‘selection’.
Iteration
A block of code is executed a certain number of times or while a condition is met. Iteration uses FOR, WHILE or REPEAT UNTIL loops
Iteration can be either:
- Count-controlled
Iteration is repeated a given number of times.
for
-Condition-controlled
Iteration continues until a given condition is met.
While
Recursion
Recursion is a programming construct in which a subroutine
calls itself during its execution
A recursive sub-routine should:
- Contain a stopping condition (base case)
-For any input value other than the stopping condition, the subroutine should call itself (recursion)
-The stopping condition should be reachable within a finite number of times
Without these characteristics a recursive subroutine may call itself indefinitely which will result in a stack overflow.
Iteration vs recursion
- more memory efficient
-recursion is not very memory efficient. Every time a recursive function calls itself, the processor needs to remember where it was before it jumps to the new copy so it knows where to return later.
-The processor also needs to remember the values of all the previous variables, as they are local to those copies of the recursive function- this is done using stacks which takes up space in memory.
if a recursive subroutine calls on itself too many times you may run out of memory and cause a stack overflow which will cause the program to crash.
local variable main points
- declared inside a subroutine
-only accessible by that subroutine
-created when the subroutine is called
-destroyed when the subroutine ends
global variable main points
- declared at the top of the program, outside of any subroutine
-accessible throughout the program
-created when the program starts
-destroyed when the program ends
what is a local variable
Local variables have limited scope which means that they can only be accessed within the subroutine in which they were defined. Therefore, multiple local variables with the same name can exist in different subroutines.
Global variable
Global variables, on the other hand can be accessed across the whole program. These are useful for values that need to be used by multiple parts of the program
Advantage to local variables
Using local variables is considered to be good programming practice because it ensures subroutines are self-contained, with no danger of variables being affected by code outside of the suoroutine.
Disadvantage to global variables
using global variables is not recommended because they can be unintentionally overwritten. As global variables are not deleted until the program terminates, they require more memory than local variables which are deleted once the subroutine has been completed.
• Excessive, unnecessary use of global variables can make programs hard to test, debug and maintain.
give two advantages of using local variables over global variables
-less memory is used
-self contained so unaffected by code outside of the subroutine
- take procedure over global variables with the same name
What is modularity
Modularity is the concept of breaking a large program or problem down into smaller chunks.
The goal is to design a program in such a way that each module carries out a single, specific task.
Procedures
• A procedure is a block of code that takes in zero, one or more parameters and performs a set task
Function
A function is a block of code that takes in zero, one or more parameters; performs a set task; and returns a value.
what is the difference between procedures and functions
Functions must always return a value while a procedure does not always have to return a value
state two advantages of recursion
• can be represented in fewer lines of code
-Easier to express some functions recursively than using iteration
state a disadvantage of recursion
-inefficient use of memory
-danger of stack overflow from it calling itself repeatedly which would cause the call stack to run out of memory this would cause the program to crash
-difficult to trace
state two advantages of a modular design
-makes a problem easier to understand and approach
-simpler to divide tasks between a team.
-easier to manage project
-self contained modules simplify testing and maintenance
-greater reusability
what does it mean to pass a parameter to a subroutine by reference
• When a parameter is passed by reference, a pointer that contains the memory address of the original variable is created.
This then means
the address in memory of the parameter is passed to the subroutine so its value outside of the subroutine will be updated
Passing parameters by value
When a parameter is passed by value, the value created for the subroutine being called is a copy of the original.
• Once it is passed in, the parameter is held in a separate memory location
and therefore, it is only available to that subroutine.
• The copy is now a local variable of that subroutine.
What is an IDE
• An IDE is a program that provides a set of tools and related functionality designed to maximise a programmer’s productivity
Ide features
-Code editors
-Error diagnostics
-Runtime environments
-Translators
-Auto-documentation
-Syntax highlighting
-Autocompletion
Code editors
• A text area for programmers to enter code directly into the IDE; often supports additional features like syntax highlighting, autocomplete and automatic indentation.
Error diagnostics
• Reports errors (mainly syntax) in the code and where they can be found; often suggests possible fixes.
Run-time environments
• Software that supports the execution and running of programs.
• Allows programmers to easily run code during development.
Translators
• A program that converts high-level code into executable machine code.
Auto-documentation
• Tracks variables declared by the programmer, modules and other special programmer comments with a view to producing documentation that aids in program maintenance, debugging and support.
what is top down design
A technique used to modularize programs in which the problem is continually broken down into sub problems, until each can be represented as an individual, self-contained module which can preforms a certain tasks