2.2 Problem solving and programming Flashcards
What are the three types of programming constructs?
sequence
branching
iteration
Explain what we mean by sequence as a programming construct
when code is executed line-by-line, from top to bottom
Explain what we mean by branching as a programming construct
when a particular block of code is met if a specific condition is met
examples are if and else statements
Explain what we mean by iteration as a programming construct
blocks of code which loop
can be either:
count controlled- blocks of code looping over a certain number of times
condition controlled- blocks of code looping until a specific condition is met
examples are DO, WHILE, FOR loops
Explain recursion
When a subroutine calls upon itself during its execution
-continues until a stopping condition is met
What are some advantages to using recursion? (2)
-code can be represented in fewer number of lines
-easier to express some functions recursively e.g. factorial
What are some disadvantages to using recursion (2)
-risk of stack overflow if memory runs out
-difficult to trace
What is a scope and how is this related to variables?
-variables can be defined with a global or local scope
-a scope is the section of code where the variable can be accessed
Explain what local variables are and their characteristics
variables which can only be accessed within the subroutine where they were defined
Explain what global variables are and their characteristics
variables which can be accessed through the whole program
What are some +/- of using local variables? (3, 1)
+won’t affect other sections of code
+deleted once subroutine ends so doesn’t take up lots of memory
+multiple local variables with same names but in different subroutines can exist
-need to keep declaring in different sections of code if needed there
What are some +/- of using global variables? (1, 3)
+useful for values that need to be used by multiple parts of the program
-if changed, it will affect other areas of code where it has been called upon
-can unintentionally edit it
-not deleted until program terminates so requires more memory
What is modular programming?
a technique used to split large/complex programs into smaller, self-contained modules
Why is modular programming useful? (3)
-easier to divide tasks between a team and manage projects
-simplifies the process of testing and maintenance (each component can be tested individually
-improves reusability of components
Explain a top down design/modules
-technique to modularise programs
-problem is broken down into subproblems until each is is represented into individual, self-contained modules which perform a specific task
-modules form blocks of code called subroutines