programming techniques Flashcards
sequence
set of program instructions are written and exceted in the order that they are written in
e,g
name = input(“please enter name: “)
print(“ shush” +name “.”)
branching(selection)
allows a program to run sequences if code depending if the condition evaluates to true of false
carried out with if/else and switch/case
iteration
section of code is repeated using a loop
can be controlled with a condition at the start or end of loop
features of iteration
count controlled:
definite iteration which is a for loop
condition controlled: indefinite iteration which is a while loop(loops till condition is met)
pre check checks condition before executing statements
pro check executes the statement and then checks the condition
recursion
when a function calls itself from within the function
benefits and drawbacks of recursion
benefit:
more natural to read
faster to write as less lines of code
suited for certain problems e,g trees
can reduce size of problem with each call
drawbacks:
Requires more memory than the equivalent iterative algorithm
More difficult to trace as each frame on the stack has its own set of variables
Usually slower than iterative methods due to maintainence of the stack
Can run out of stack memory due to too many calls causing it to crash
global variables
can be accessed throughout the program
they are declared or set outside any subprograms and subroutines
dangerous as it is easy to overlook themwhen they are changed
make debuggin hard
drawbacks of global variables
Global variables make it difficult to integrate modules
they increase complexity of a program
they may cause conflicts with names written in other modules
makes debuggin harder
local variables
are set or declared within a function or a subprogram
can only be accessed from with that subprogram
can be used as a parameter
IDE
set of tools to develop a program
Features of IDE
Error diagnostics - can locate and report errors
Code can be examined as it is running which allows logical errors to be pinpointed
Debugging tools allow inspection of variable values this can allow run-time detection of errors
IDE - Breakpoints
Use to test the program works up to specific points
Check variable contents at specific points
Can set a point where the program stops running
IDE - stepping
Can set the program to run line by line
Slow down/watch execution
Find the point where an error occurs
modularity
programs are written in separate sections as it is easier to test
functions
are subprograms which can return a value but not always