2.2.1 Flashcards
sequence
Code is executed line-by-line, (one after the other) from top to bottom.
Qty = input() Total = qty * price
branching
- Branching (selection)
Allows program to change directions depending on the outcome of a condition (Boolean expression).
A certain block of code is run if a specific condition is met, using IF statements.If a > b Print(“A is bigger than b”)
iteration
- Iteration (looping)
A block of code is executed a certain number of times or while a condition is met. Iteration uses FOR, WHILE or REPEAT UNTIL loops.
Iteration can be either: - Count-controlled
Iteration is repeated a given number of times.
for i in range (0,10):
print i
next i - Condition-controlled
Iteration continues until a given condition is met.
while i <= 20:
print “Not true”;
i=i+1
endwhile
why while instead of for
becasue the user needs to keep enetring their password until its correct and a while loop will
continue looping until the correct password is inputted. As well as this the number of attempts isnt known
the program need sto carry on looping until the correct input is entered and a while loop will carry on looping until the correct input is eetered, a for loop will repeat only a certain number of times so it wouldnt work. As well as this the number of attempts isnt known
recursion
Is when a subroutine (often a function) call itself from within its own subroutine. Recursive function characteristics:
Contains a topping condition
For any input value other than stopping condition, the subroutine should call itself(recursion).
The stopping point should be reachable within a finite number of times.
Without these, stack overflow can occur.
Each time the function calls itself, a new stack frame is created within the call stack, where parameters, local variables and return addresses are stored. This information allows the subroutine to return to that particular point during
its execution.
A finite number of stack frames are created until the stopping condition, or base case, is reached at
which point the subroutine unwinds.
recursion adv and dis
The advantage of using recursion for certain problems is that they can be represented in fewer lines of code, which makes them less prone to errors.
Inefficient use of memory. If the subroutine calls itself too many times, there is a danger of a stack overflow,
which is when the call stack runs out of memory. This would cause the program to crash.
Another problem with recursion is that it is difficult to trace, especially with more and more function calls.
recursion vs iteration
global
unnecessary use of global can make program hard to test, debug and maintain.
local
difference between local and global
function
Is a block of zero that performs a specific task, taking in zero, one or more parameters and also returning a value.
procedure
Is a block of code that performs a set task, taking in zero, one or more parameters.
passing by reference
Memory location of data is sent
Changes are made to the original data
Changes remain after the subprogram exits
passing by value
A local copy of the data is used
Data is discarded when the subprogram exits
Does not override/change the original data
why by reference and not by value
ByRef changes the value in the variable passed.
ByRef passes the address/location [1]
ByVal only a copy of the data is passed [1]
ByVal the change would be lost when the procedure ended [1]