2.2.1 Programming Techniques Flashcards
(25 cards)
Explain the difference between branching and iteration [2]
- Branching decides which code is run
- Iteration repeatedly runs the same code in the same sequence
What are two ways to write selection statements? [2]
- If… elseif… else…
- switch… case
Identify the type of branching statement used in the function:
function GCD(num1, num2)
if num2==0 then
return num1
else
return GCD(num2, num1 MOD num2)
endif
endfunction
[1]
If
State one benefit and one drawback of using iteration instead of recursion [2]
Benefit:
1. The program can run faster
2. Cannot run out of stack space/memory
3. Easier to trace/follow
Drawback:
1. Iteration can lead to lengthier code
2. Iteration can lead to code that looks more complex / is harder to understand
3. Some problems are more elegantly coded with a recursive solution
Compare two differences between recursion and iteration [4]
2 of the following:
1. Recursion uses more memory…iteration uses less memory
2. Recursion declares new variables…iteration reuses the same variables
3. Recursive can run out of memory space…while iteration cannot run out of memory
What is a data type?
A classification of data into groups according to the kind of data they represent
What is casting?
When you convert one data type to another data type
What does the MOD operator return?
The remainder when one numerical value is divided by another
Explain how programmers make use of reusable components when developing large programs
Software is modular, an example being an object. Modules can be transplanted into new software.
What does a programming construct do?
Determines the order in which lines of code are executed
Name 3 programming constructs
- Sequence
- Iteration
- Branching (selection)
What is sequence?
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 written from the first line of code to the last line of code
What is branching (selection)?
When the flow of the program is interrupted and a condition is tested. The outcome of this condition will then determine which lines or block of code is run next
What is iteration?
Iteration is repeating a line or a block of code using a loop
What is modularity?
Where problems are broken down into more manageable pieces
What are subroutines (modules)?
Standalone blocks of code that when called, will complete a task
What is one difference of procedures and functions?
Functions will return a value. Procedures can also be called to complete a task, however, they do not return a value
A pseudocode recursive function, generate, is shown.
function generate(num1 : byval)
if num1 > 10 then
return 10
else
return num1 + (generate(num1 + 1) DIV 2)
endif
endfunction
The parameter, num1, is passed by value. Explain why the parameter was passed by value instead of by reference. [2]
If the value is passed by value, num1 will not be overridden as it is a copy of the parameter that is used, and this will produce the correct output.
What is recursion?
When a function calls itself to solve a problem or execute a task
What are the benefits and drawbacks of recursion compared to iteration?
Benefits:
1. Recursion can often be expressed more concisely than iteration
2. Recursion can lead to less complex code
Drawbacks:
1. Can be harder to track the state of the program
2. It can run out of memory space
What is a global variable?
A variable declared outside any modules such as functions or procedures.
What are 2 benefits and drawbacks of global variables?
Benefits:
1. The global variable only needs to be declared once.
2. You don’t need to keep passing parameters between the different modules.
Drawbacks:
1. The global variables are always stored in memory while the program is running which can use up memory.
2. Makes it difficult to test a single block of code as the programmer will need run the entire program to setup the global variables.
What is a local variable?
A variable declared within a specific scope, such as a function or a code block that are accessible only within the block in which they are defined.
What are 2 benefits and 2 drawbacks of local variables?
Benefits:
1. Local variables have a limited lifetime, and their memory is automatically reclaimed when the code block ends, making them memory efficient.
2. Local variables encapsulate data within a particular function or block, providing data hiding and preventing unintended access from other parts of the program.
Drawbacks:
1. Repeatedly creating and destroying local variables within a loop or recursive function can incur unnecessary memory overhead.
2. Excessive use of local variables within deeply nested blocks can lead to code clutter and reduce code readability.