What’s a…
Programming Construct purpose?
Simplifies problem = easy to understand & program.
What’s…
4 Programming Constructs
Structured programming
Recursion is a type of iteration
What’s…
Sequential programming?
Programming construct (1/4)
Code executed:
What’s…
Selection?
Programming construct (2/4)
Code runs if specific condition met.
Also known as ‘Branching’
What’s…
Iterative programming
Programming construct (3/4)
Code executed X no. of times or while condition met.
Uses FOR, WHILE or REPEAT UNTIL loops.
How’s…
Iteration controlled?
What’s a…
Count-controlled loop?
Iterative programming
How programmed?
Repeated X no. of times.
for i in range (0,10):
— print i
next i
What’s…
Condition-controlled loop?
How programmed?
Continues until a condition met.
while i <= 20:
— print “Not true”;
— i=i+1
endwhile
What’s…
Recursion?
Programming construct (4/4)
What’s stopping condition?
Subroutine calls itself during its execution.
Condition met = recursion stops.
Why use…
Recursion over Iteration?
Both produce same result
What’s…
Recursion’s main advantage?
Why need to clearly defined?
Uses fewer lines of code = less errors.
NB: Not all problems
Reach stopping condition after a finite no. of function calls.
How’s…
Recursion programmed?
function factorial(number)
— if number == 0 or 1:
—— return 1
— else:
—— return number * factorial(number - 1);
— endif
end function
What’s a…
Stack frame?
Recursive programming
When?
Stores data within call stack - belonging to subroutine:
Each time fuction calls itself
What’s a…
Call Stack?
Recursive programming
Data structure storing:
From currently running subroutines.
When does…
Subroutine unwind?
Recursive programming
When base case/stopping condition reached
Finite no. of stack frames are created.
What’s…
Recusion disadvantages?
Recursive programming
What’s…
Stack overflow?
Recursive programming
Subroutine calls itself too many times = call stack runs out of memory = program crashes.
What’s…
Tail recursion?
More efficient = use less stack space.
What’s…
Scope?
What’s 2 types?
Section of code where variable = available.
What’s a…
Local variable?
diff subrouines with same local variable name != effect others
What’s…
Local variables benefits?
What’s…
Global variables?
Variables in main body = automatically global.
What’s…
Global variables negatives?
Positives?
What’s…
Modular programming?
Technique splits large, complex programs –> smaller, self-contained modules.