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]
Compare the use of parameters to global variables in recursive functions.
Parameter allows a value to be sent to a sub-program
Global variables can be accessed throughout the scope of the program
Local variables can only be accessed within the scope of the sub-program it’s defined within – a parameter becomes a local variable in the function
If global, equivalent of by reference -value would be over-ridden
Global variable takes more memory than a local variable
In recursion, each call produces a new local
variable for num1
Global would require altering the algorithm as the value would be over-ridden on each call
Global would mean that memory space is kept
throughout the running of the program, not just the sub-program
Parameter enables memory to be reallocated
Many more memory spaces needed for parameter
IDE
Integrated Development Environment (IDE), is a program which provides a set of tools to make it easier for programmers to write, develop and debug code.
Examples of IDEs include PyCharm, Eclipse, IDLE and Microsoft Visual Studio.
Common features of IDEs include:
- Stepping
This allows you to monitor the effect of each individual line of code by executing a single line at a time. - Variable watch
Sometimes used to pinpoint errors, this is a useful feature to observe
how the contents of a variable change in real-time through the execution of a program. - Breakpoint
IDEs allow users to set a point in the program at which the program will stop. This can either be based on a condition or set to occur at a
specific line. This can help to pinpoint where an error is occurring. - Source code editor
The editor aims to make the coding process easier by providing
features such as autocompletion of words, indentation, syntax
highlighting and automatic bracket completion. - Debugging tools
Some IDEs also provide run-time detection of errors with a guide as to where in the code they are likely to have occurred through line
numbers and highlighting.
Iteration adv and dis
Variable
Identifier of a memory location used to store data