2.2.1 Programming Techniques

Download: 2.2.1 Programming Techniques

Variables & Constants:

Variables are identifiers (name) given to memory locations whose contents will change during the course of the program. For example:

Name = input(“Enter Your Name: “)

A Constant is a named location that can be used to store value that never changes while the program is running. The advantage of using a constant in a long and complex program, is that there is no chance that a programmer will accidentally change its value by using the identifier for a different purpose.

The process of setting a value to a Variable or a Constant is known as an assignment. This is usually shown using an equals sign. For example: name = TheLittleJedi

It is also important to follow a Universal Standard when it comes to using variables and Constants. It will help your code to be more readable and understandable by another programmer. The guidelines can be:

  • Starting all variable names with a lowercase letter
  • Do not use underscores in the middle of variable names but instead use ‘camelCaps’ to separate parts of a variable name.
  • Do not use overly long variable names to help avoid confusion.
  • Writing in all uppercase for Constant values, which are then instantly identifiable.

Comments:

Comments are lines of code that is completely ignored by the computer when the program is executed.

The main purpose of comments is that they can be used to add description/explanation to your code in order to make it more readable towards other users.

They can also be used to leaves or reminders to yourself as you work on a complex part in a program.

In Python, the sign for Comments is ‘ # ‘

Program Constructs:

There are just three basic programming constructs: Sequence, Selection & Iteration.

  • Sequence is just two or more statements executed one after the other.
  • Selection statements (also known as Branching) are used to select which statements will be executed next, depending on some condition. Conditions are formulated using relational operators. (if & else statements)
  • Iteration means repetition. This involves performing a loop in a program to repeat a number of statements. (while loops, for loops & repeat until loops)

Relational Operators:

  • > Greater than
  • < Less than
  • >= Greater than or equal
  • <= Less than or equal
  • == Equal
  • != Equal

Arithmetic Operators:

  • + Addition
  • – Subtraction
  • * Multiplication
  • / Division
  • % Modulus

Modularity (Functions & Procedures) (Also known as Subroutines)

A Module/Subroutine is a named block of code, which performs a specific task within a program.

  • They make the program easier to read as there are fewer long blocks of code to understand
  • It makes a program more efficient as blocks of code can be written once and reused many times.
  • It is make a program more reliable as each subroutine can be individually tested to make sure it works as intended. (tried and tested).
  • It allows the use of effective teamwork when developing a large program. This is because different programmers can work on separate programs in their own modules. This can speed up the development of the program.

There are two types of subroutines:

  • A Function is a subroutine which returns a value.
  • A Procedure is a subroutine which does not return a value.

Parameters are special variables that are used to pass values into a subroutine, while Arguments are the actual values that are passed in.

Local & Global Variables:

Parameters can be passed through subroutines in two ways:

  • By Value | A copy of the variable will be passed into the subroutine. Changing this copy will not affect the original variable in the main program. This is an example of a local variable. However, if the copied variable has been changed when passed through the subroutine and you want to use it in the main program. You will need to return it.
  • By Reference. | The original variable will be passed into the subroutine. The means that the ‘address’ of the variable is passed through the subroutine. This means any changes that occur to the variable in the subroutine will be affected in the main program. This is an example of a global variable.

Recursion:

Recursion is a feature of functions. A function is only recursive when it has recursion in it. This means the name of the function is used within the same function. A recursive function calls itself.

  • Recursion is a way of making code repeat itself. This produces repetition.
  • Recursion is an alternative to iteration. But they tend to be shorter and faster in some cases.
  • Anything that can be done with recursion can be done with iteration. However, it is more complicated.

Recursion is not very memory efficient. This is because a function uses an area of memory, which is set aside for that function. The parameters and local variables of the function are stored inside that area of memory. A recursive function opens another copy of the function. This means each time the function calls itself, it creates a new area of memory. As a result, a recursive function uses up much more memory than an ordinary function.

In a normal function, the last line is the word Return plus an expression. It would return the value of that expression. But in a recursive function, the expression that follows Return is the name of the function.

However, there has to be a way to stop the recursion otherwise it would go on forever or crash the program. As a result, every recursive function has an IF statement. If this IF statement is True, then the function returns a value without recursion. This is called the Base Case.

  • The Base Case stops the recursion.

Integrated Development Environment: (IDE)

Integrated Development Environments are software packages that facilitate software development. They provide a number of features to make programming easier:

  • Code editor | This is a text editor which allows you to write the source code. This usually contains extra features such as: Syntax Highlighting & Code-Completion.
  • Debug Tools | IDEs usually contain error diagnostics, which highlight syntax errors in real time as you program. They may also contain a debugger, which allows you to run your code line-by-line to find faults.
  • Run-Time Environment (RTE) | This allows you to run programs to test how well they work.
  • Output Window | To show the output of the program when it is executed.
  • Auto-Indentation | It made automatically intend code for you or won’t you execute the program without proper indentation like Python.
  • Line Number/Character Number | Tells you how many lines of code there is plus how many characters.
  • Translators | The IDE will usually have an interpreter, compiler or assembler built in.

The IDE may use some other error diagnostic tools to help you solve complex logic errors as these are usually more difficult to find than syntax errors. For example:

  • Breakpoint | You can set a breakpoint in the program which will cause the program to stop on a specific line, so that you can see whether it reaches that line.
  • Watch | You can set a watch on a variable so that its value is displayed each time it changes.
  • Step Through | You can step through a program a line at a time so you can see what is happening.

Translators & Facilities of Languages

  • High-Level Language – This is language that humans can easier read and write, however computers need to translate it into machine code before they can read and run it.
  • Low-Level Language – This is hard for humans to understand, read or write. But, it is easier for a computer is run.
High-Level Language:Low-Level Language:
·         One instruction of High-Level Code represents many instructions of machine code.·         One instruction of assembly code usually only represents one instruction of code.
·         The same code will work for many different machines & processors.·         Usually specifically written for one type of processor. Won’t work on any others.
·         The programmer can easily store data in lots of different structures. For example: Arrays & Lists.·         The programmer needs to know the internal structure of the CPU and how it manages memory in order to process machine code effectively.
·         Code is easy to read, understand and modify.·         Code is very difficult to read, understand and modify.
·         It must be translated into machine code.·         Commands in machine code can be executed directly by the CPU without the need for a translator.
·         You don’t have much control of what the CPU actually does, so programs will be less memory efficient and slower.·         You can control exactly how the CPU operates and how it will use memory. Therefore, programs will be more memory efficient and faster.
Compiler:Interpreter:
Translates all of the source code at the same time and creates one executable file.Translate and runs the source code one instruction at a time, but doesn’t create an executable file.
Only needed once to create a executable file.Needed every time you want to run the program.
Returns a list of errors for the entire program once compiling has completed.The interpreter will return the first error it finds and then stop. This is useful for debugging.
Once compiled, the program runs extremely quickly. But compiling can take a long time.Programs will run more slowly because the code is being translated as the program is running

 Object Oriented Programming:

Object Oriented Programming (OOP) refers to a type of software design in which programmers define the data type of a data structure, and also the types of functions that can be applied to the data structure.

An object-oriented program is composed of a number of interacting objects, each of which is responsible for its own data and the operations on that data.

Classes:

In object oriented programming, we define new data types called ‘Classes’. This is defined as a ‘blueprint’ or ‘template’ for an object, and it defines the attributes and behaviour (methods) of objects in that class.

  • We decide the attributes of the class.
  • We decide the methods of the class.
  • Which then we make variables, which are instances of the new class.

Definitions:

  • Class: A data type defined by the programmer (can be considered a template for new variables.)
  • Instance: A new variable of that class.
  • Attribute: A data value stored in the variable.
  • Method: An action that can be carried out by variables of that class.

Defining a class can be visualised like this:

Name:
Attributes:
Methods:

Loading