2.2.1 FINAL Programming techniques
No description yet. This curated deck is ready for daily study.
Card preview (24)
What is a data type?
A classification of data into groups
What is an integer?
Used to represent whole numbers, either positive or negative
What is a real?
Used to represent numbers with a fractional part, either positive or negative
What is chr?
Used to represent a single character such as a letter, digit or symbol
What is a string?
Used to represent a sequence of characters
What is boolean?
Used to represent true or false values
What is casting?
When you convert one data type to another data type
What is a programming construct?
Determines the order in which lines of code are executed
State the 3 types of programming constructs
Sequence, Branching, Iteration
What is a sequence?
• Refers to lines of code which are run one line at a time • The lines of code are run in the order that they are written from the first line of code to the last line of code
What is a selection?
• The flow of the program is interrupted and a condition is tested • Outcome of the condition will then determine which lines/block of code is run next. Two ways to write selection statements: • IF statement: test conditions sequentially SWITCH statement: test an expression against multiple possible constant values
What is an iteration?
• Repeating a line or block of code using a loop. It can be: • Count controlled: When the code is repeated a fixed number of times (for loop) • Condition controlled: When the code is repeated until a condition is met (while loop)
What is a SWITCH/CASE statement?
• Type of conditional statement that provides an alternative way to perform multiple comparisons based on the value of an expression • Useful when you have a single expression that you want to compare against multiple possible values
What is a for loop?
Count controlled loop that will repeat a fixed number of times
What is a while loop?
Condition controlled loop that will repeat until a condition is met - Provides a flexible way to handle situations where the number of iterations in unknown in advance
What is modularity?
Where problems are broken down into more manageable pieces - Each piece of the problem should be carried out by one single subroutine
What are subroutines?
• Standalone blocks of code and when called, they will complete a task • The promote code reusability, modularity and organisation, enabling a programmer to write efficient and maintainable programs
What is a function?
Block of code that takes in 0, 1 or more parameters, performs a set task and return a value
What is a procedure?
Block of code that takes in 0, 1 or more parameters and performs a set task
What is a parameter?
Pieces of data which are passed into the function or procedure to allow it to complete a task
What are the two ways of passing a parameter?
• Passing by value • Passing by reference
What does it mean by parameter passing by value?
• A copy of the actual value is made and assigned to the function’s parameter • Any changes to the parameter within the function’s scope do not affect the original value outside the function • Function works w/ and changes its own local copy of the value • Suitable when you want original data to be protected from being modified within the function • When function ends, copy of the value is destroyed • Can lead to performance overhead when working with large data structures since a copy is made
What does it mean by parameter passing by reference?
• Function receives the memory address of the value rather than a copy of the value • Any changes made to the parameter within the function directly affect the original value outside the function • Both the function and the calling code share the same memory location • Efficient for working w/ large data structures • Can lead to unexpected side effects if not handled carefully, as changes made within the function affect other parts of the program
What is recursion?
When a function calls itself to solve a problem or execute a task - Uses idea of self-reference to break down complicated problems into more manageable subproblems