2.2.1: Programming Techniques

Integrated Development Environment, IDE:

An IDE is software that enables you to enter, edit, compile and run programs. Many also have debugging faclities to help find errors.

Debugging facilities include:

  • Set a breakpoint which will stop the program on that line.
  • Set a watch on a variable so its value is displayed when it changes.
  • Step through a program one line at a time
  • Trace the execution of a program- e.g: display a message when a particular statement is executed.

IDE’s offer many specific features designed to make entering code as easy as possible:

  • Line Numbers
  • Automatically indent code
  • auto-complete commands
  • comment/uncomment a region

The IDE will also identify and tell you the location of a syntax or logical errors.

This image has an empty alt attribute; its file name is image-2.png

The 3 basic programming constructs

Sequence – The order in which the code is executed

Selection – Boolean instructions such as if, else

Repetition – Iteration/Recursion.


Algorithms

Algorithm – A sequence of instructions that can be followed to solve a problem.


Pseudocode

Pseudocode is used to write instuctions in statements that are between english and programming language.

There arent strict rules to stick to .

It acts as an aid to thinking out steps before you start coding.

Comparing:


Variables and Assignment


Variables and Constants

The value of a variable can be changed while the program is running.

To change the value of a constant, you need to change it in the source code then recompile. A constant cannot be a target of an assignment, it will cause an error.

Constants reduce the risk of errors by reducing accesss to memory location.

Local Variable – Declared and used only inside the sub-routine that it was declared in. Created when routine is called and destroyed when it ends.

Global Variable – Declared at the top of a program outside of sub-routines. Usable throughtout the program. Created when program starts and destroyed when it ends.


The mod and div operators

mod is used to find the remainder in integer division

div returns the integer part of the division

x = 17 mod 3 : sets x = 2

y = 17 div 3 : sets y = 5


Iteration

This means repetition. In programming such as python, you can make an iteration by creating a loop with, for example, a while loop. This is a much more efficient alternative than writing the instruction multiple times.


Subroutines

A subroutine is a set of instructions with a name.

Procedures and functions are both types of subroutine. A function is a subroutine that returns one or more values. In most languages subroutines are referred to as functions even if no value is returned. Procedures dont have to return a value.

There are many built in functions such as len() or round(). And you can also import additional modules from the module library. E.g in python you can import random to get randint()

Parameter and arguments are closely defined. E.g:

function(x) = x*x

The x is the parameter and if i were to call the function, function(8), the 8 is the argument.

Parameter – The defenition of the argument

Argument – This is the value passed onto the function

However these defenitions can be used interchangeably.

The scope of a variable means where in the program it can be used. E.g: the scope of a local variable is the subroutine in which it was declared. It does not exits outsdie of that subroutine. The scope of a global variable is everywhere.


Parameter Passing

This is where you give a function a parameter while it is running. This enables the user to specify the parameter.

E.g: To show the 10 times table:

To let the user choose which times table to display it without parameter passing would look like this:

Using parameter passing:

Parameter passing is essentially timestable(number). You can have multiple parameters, seperated the commas.


Recursion

Essentially “A subroutine/function which calls itself.”

The act of the subroutine calling itself from within its own subroutine is recursion

A recursive subroutine must have these 3 characteristics:

  • Must contain stopping condition (base case)
  • For any input values other than the stopping condition the routine must call itself (recursion)
  • The stopping condition must be reachable after a finite number of times. (or recieve an error called stack overflow).

Downsides to Recursion:

  • Often requires extra storage space
  • Can become endless without a strong exit condition
  • Typically not efficient in speed and time.

Therefore best to use “normal” iterative approach where possible.

Example: Countdown function

def countdown_rec(n):
    if n == 0:
        exit
    else:
        print(n)
        countdown_rec(n-1)

Iteration Vs Recursion:

Recursion requires more memory than iteration as it needs to create multiple instances of itself whereas iteration runs through it one at a time. However iteration usualy needs more lines of code than recursion. Generally iteration is preferred over recursion.

Leave a comment

Design a site like this with WordPress.com
Get started