This document can summarize the unit 2.2 in computer Science, which covers:
Programming techniques
Constructs
Recursion
Variables
Integrated Development Environment
Object Oriented Techniques
Computational Methods
Programming Techniques:
Programming Constructs:
A crucial part of solving a problem is simplifying it to represent it in a way that makes it easier to
understand and therefore program. The following constructs are used to represent a program’s
control flow in a popular subsection of procedural programming called structured programming.
Sequence
Code is executed line-by-line from top to bottom.
Branching
A certain block of code is executed if a specific condition is met, using IF statements or
selection statements.
Iteration
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.
o Count Controlled:
Iteration is repeated a given number of times.
For I in range (0,10):
Print i
Next i
o Condition Controlled
Iteration continues until a given condition is met:
While I <= 20:
Print “Not true;
I=I+1
endwhile
Recursion:
Recursion is a programming construct in which a subroutine calls itself during its execution. This
continues until a certain condition, which is called the stopping condition, is met, at which point, the
recursion stops. Recursion produces the same result as iteration but is more suited to certain
problems which are more easily expressed using recursion.
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. However, it is essential that recursive subroutines
are clearly defined so as to reach a stopping condition after a finite number of function calls.
A common example of a naturally recursive function is factorial, shown below:
Function factorial(number)
If number == 0 or 1:
Return 1
Else:
Return number + factorial(number – 1);
Endif
End function
, 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. A call stack is a Last in First Out data
structure which stores details about the order in which subroutines have been called. 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. This refers to the process of information from the call stack being
popped off the stack.
There are some disadvantages to recursion, the biggest being its 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. Tail recursion is a form of
recursion which is implemented in a more efficient way in which less stack space is required.
Another problem is recursion is that it is difficult to trace, especially with more and more function
calls.
Global and Local Variables:
Variables can be defined as either global or local scope. Scope refers to the section of code in which
the variable is available.
Local variables have limited scope which means that they can only be accessed within the block of
code in which they were defined. If a local variable is defined within a subroutine, it can only be
addressed in that subroutine. Therefore, multiple local variables with the same name can exist in
different subroutines and will remain unaffected by each other. Using local variables is considered to
be good programming practice, because it ensures subroutines are self-contained, with no danger of
variables being affected by code outside of the subroutine.
Global variables, on the other hand, can be accessed across the whole program. All variables used in
the main body of the program are automatically declared to be global. These are useful for values
that need to be used by multiple parts of the program. On the whole, however global variables
aren’t recommended as they can be unintentionally overwritten and edited. As global variables
aren’t deleted until the program terminates, they require more memory than local variables which
are deleted once the subroutine has been completed.
In the event that a local variable exists within a subroutine with the same name as a global variable,
the local variable will take precedence.
Modularity, Functions and Procedures:
Modular programming is a programming technique used to split large, complex programs into
smaller, self-contained modules. Modularity is essential to making a problem easier to understand
and approach. A modular design also makes it easier to divide tasks between a team and manage,
whilst simplifying the process of testing and maintenance, as each component can be dealt with
individually. This improves the reusability of components, as once a module has been tested, it can
be reused with confidence.
A popular technique used to modularise programs is called the top-down approach, in which the
problem is continually broken down into sub-problems, until each can be represented as an
individual, self-contained black box which performs a certain task.
This process is also known as stepwise refinement. These modules form blocks of code called
subroutines, which can be categorised as either functions or procedures.
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller michaellawther. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $9.82. You're not tied to anything after your purchase.