2.2.1 Programming Techniques Flashcards
Sequence
Executing instructions one after another
Selection (branching)
- IF statement
- A construct that allows a program to change direction depending on the outcome of a condition
Iteration
Enables us to repeat sections of code
When is a FOR loop (count-controlled loop) used?
When the required number of iterations is known ahead of the execution
When is a WHILE loop (condition-controlled loop) used?
When the required number of iterations is unknown as the variable used to determine when the iteration ends is changing within the iteration itself
Nest structure
Programming constructs inside one another
Recursion
When a subroutine calls itself from within another subroutine
What 3 characteristics should a recursion subroutine have?
- Contains a stopping condition (base case)
- For an input value other than the base case, the subroutine should call itself
- The stopping condition should be reachable within a finite number of times
Without the 3 characteristics, what would the recursion subroutine result in?
A stack overflow
Variable
Used to store information to be referenced and manipulated in a computer program
Local variable (4)
- Declared inside a subroutine
- Only accessible by that subroutine
- Created when the subroutine is called
- Destroyed when the subroutine ends
Global variable (4)
- 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
Scope of a variable
- Refers to where the variable can be accessible in the code
Procedure
- Block of code that takes in zero, one or more parameters and performs a set task
Function
- Block of code that takes in zero, one or more parameters and performs a set task
- RETURNS A VALUE