2.2.1 Programming Techniques Flashcards
Sequence
Code is executed line-by-line, from top to bottom
Selection
A certain block of code is run if a specific condition is met, using IF
statements. This is also known as ‘branching’.
Iteration
Can be either:
Count-controlled
- Block of code executed a certain number of times
Condition-controlled
- Block of code is executed while a condition is met
Uses FOR, WHILE or REPEAT UNTIL loops
Recursion
- Programming construct in which a subroutine calls itself during its execution
- Continues until a stopping condition is met
- produces the same result as iteration, but is more suited to certain problems
Recursion - Advantages
- Can be represented in fewer lines of code (for some problems), making them less prone to errors
- Easier to express some functions recursively eg. Factorial
Recursion - Disadvantages
- Inefficient use of memory
- Risk of stack overflow if memory runs out
- Difficult to trace (especially with more and more function calls)
Variables
- Variables can be defined with either global or local scope
- Scope is the section of code in which the variable can be accessed
- A local variable within a subroutine takes precedence over a global variable with the same name
Local Variables
- Can only be accessed within the subroutine in which they were defined
- Multiple local variables with the same name can exist in different subroutines
- Are deleted once subroutine ends
- Using local variables ensures subroutines are self-contained - good
programming practice
Global Variables
- Can be accessed across the whole program
- Useful for values that need to be used by multiple parts of the program
- Danger of being unintentionally edited and overwritten
- Not deleted until program terminates, so require more memory
Modular Programming
- A programming technique used to split large, complex programs into smaller, self-contained modules.
- Modularity is essential to making a problem easier to understand and approach.
Benefits of a Modular Approach
- Easier to divide tasks between a team and manage projects
- Simplifies the process of testing and maintenance, as each component can be dealt with individually
- Improves reusability of components - once a module has been tested, it can be reused with confidence
Top-Down Approach
- Technique used to modularise programs
- Problem is broken down into sub-problems, until each is represented as an individual, self-contained module which performs a certain task
- Modules form blocks of code called subroutines
Functions vs Procedures
- Both named blocks of code that perform a specific task
- Procedures do not return a value
- Functions must always return a single value (some languages, like Python, allow functions to return multiple values using tuples)
- Parameters can be passed into a subroutine either by value or by reference
Passing By Value
- A copy of the value is passed to the subroutine and discarded at the end
- Its value outside of the subroutine remains unaffected
Passing By Reference
- Address of parameter is given to the subroutine
- Value of the parameter will be updated at the given address
IDE
Program which provides a set of tools to make it easier for programmers to
write, develop and debug code
Benefits of Using an IDE to Write Code
- Source Code Editor
- Autocorrect
- Autocompletion
- Pretty Printing
- Syntax Highlighting
- Auto-documentation
- Code Editors
- Translators
Source Code Editor
Benefits of Using an IDE to Write Code
The editor aims to make the coding process easier by providing features such as autocompletion of words, indentation, syntax highlighting, and automatic bracket completion.
Autocorrect
Benefits of Using an IDE to Write Code
Automatically corrects typos and common syntax errors as you type, enhancing productivity and reducing the number of minor errors.
Autocompletion
Benefits of Using an IDE to Write Code
Provides suggestions for code completion based on the context, which helps speed up coding by reducing the amount of typing and preventing syntax errors.
Pretty Printing
Benefits of Using an IDE to Write Code
Automatically formats the code to be more readable with proper indentation and line breaks, making it easier to understand and maintain.
Syntax Highlighting
Benefits of Using an IDE to Write Code
Uses different colors and fonts to distinguish various elements of the code (e.g., keywords, variables, strings), improving readability and helping to quickly identify errors.
Auto-documentation
Benefits of Using an IDE to Write Code
Generates documentation automatically based on the code and comments, which helps keep the documentation in sync with the code and improves understanding for other developers.
Code Editors
Benefits of Using an IDE to Write Code
Provide an environment for writing and editing code with features that enhance productivity, such as syntax highlighting and autocompletion.