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.