Unit 2.2.1 - Programming Techniques Flashcards
What are the three programming constructs?
- Sequence
- Selection
- Iteration
What is sequence?
Instructions in a program are executed in the order they have been written.
What is iteration?
It allows sections of code to be repeated.
What is selection?
A program that changes direction depending on the outcome of a condition. They are usually shown using Boolean expressions (IF statement).It allows sections of code to be repeated.
What are the two types of loops?
- Count - controlled loop (FOR)
- Condition - controlled (DO UNTIL/WHILE)
What is a count-controlled loop?
A loop which is used when the required number of iterations is known ahead of execution.
What is a condition-controlled loop?
A loop with a condition at the start to determine if the iteration should occur or not. As long as the condition is met the iteration continues.
It is used when the number of required iterations are not known.
What is a nested construct?
- A programming construct that is included within another programming construct.
- It enables powerful yet simple programming.
- It reduces the amount of code needed, while making it simple for a programmer to debug and edit
What is recursion?
When a subroutine (often a function) calls itself from within its own subroutine.
What characteristics should a recursive subroutine exhibit?
- It contains a terminating condition (base case).
- For any input value other than the terminating condition, the subroutine should call itself.
- The base case should be reached within a finite number of times.
What would happen if there wasn’t a base case, or the base case cannot be achieved in a finite number of times?
The subroutine would call itself indefinitely, taking up memory and resulting in a stack overflow. This could cause the program to crash.
Why is recursion not very efficient?
Every time a recursive function calls itself, the processor must remember the address it must jump back to later and the previous variables by using stacks, which takes up space in memory.
When is recursion needed rather than iteration?
- Tree traversal algorithms
- Performing a flood fill in a graphics application
What is the scope?
The scope of a variable refers to where it is accessible in the code. The scope is global or local.
What is a local variable?
- Declared inside a subroutine.
- Only accessible by that subroutine.
- Created when the subroutine is called.
- Destroyed when the subroutine ends.
What is a global variable?
- Declared at the top of a program, outside of any subroutine.
- Accessible throughout the program.
- Created when the program starts.
- Destroyed when the program ends.
What are the advantages of local variables?
- Memory efficient
- Avoiding naming conflicts and errors
- Easier to debug
What are the disadvantages of local variables?
- Limited scope
- Difficult to reuse/share data between different parts of a program
- More difficult to debug (difficult to identify and fix errors)
- May slow execution
What are the advantages of global variables?
- Can be accessed from anywhere in a program
- Can share data between functions
- Stores persistent data
- Accessed from any function
What are the disadvantages of global variables?
- Any change in a global variable is propagated throughout the program
- Make the code less modular, flexible and scalable
- Name collisions
- Logic errors by modifying global variable in one function
- Takes up memory space