Programming techniques Flashcards
Name the 3 programming constructs
- Sequence
- Branching
- Iteration
Which 2 categories of loop is iteration split up into?
- Count-controlled
- Condition-controlled
Describe how the branching program construct works
A certain block of code is run if a specific condition is met, using IF statements
What is recursion?
A programming construct in which a subroutine calls itself during its execution until the stopping condition is met
What is the base case in recursion?
A condition that must be met in order for the recursion to end
State 2 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
- Difficult to trace
Give 2 pieces of information that are stored on the call stack
- Parameters
- Return addresses
- Local variables
Define scope
The section of the program in which a variable is accessible
What is meant by a local variable?
- Limited scope
- Set or declared inside a function or other subprogram
- Can only be accessed from within that subprogram
What is meant by a global variable?
- Can be accessed throughout a program
- Declared or set outside any functions or subprograms
Give 2 advantages of using local variables over global variables
- Less memory is used
- Self-contained so unaffected by code outside of the subroutine
- Take preconditions over global variables with the same name
What is top-down design?
A technique used to modularise programs in which the problem is continually broken down into sub-programs, until each can be represented as an individual, self-contained module which performs a certain task
Give another name for top-down design
Stepwide refinement
State 2 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 is the difference between procedures and functions?
- Functions must always return a single value
- Procedure does not always have to return a value
What does it mean to pass a parameter to a subroutine by reference?
- Address of the variable is passed to the subprogram
- If a variable is changed by the subprogram, it stays changed
What does it mean to pass a parameter to a subroutine by value?
- When you pass a parameter a copy is made for the subprogram
- Changing the value in the subprogram does not change the original value
State 2 features of IDEs
- Stepping
- Variable watch
- Breakpoint
- Source code editor
- Debugging tools
What is encapsulation in object-oriented programming?
When attributes are declared as private so can only be accessed and edited by public methods
Describe the purpose of encapsulation in object-oriented programming
Program complexity is reduced by protecting data from being accidentally edited by other parts of the program
What is meant by inheritance?
Allows a class to inherit all methods and attributes of a parent class (as well as having its own)
What is meant by polymorphism?
The ability to use the same code to process different objects according to their type