# Computer Science Topic 2 - Programming

## Variables and Constants

A variable is a memory location which contains a value that changes when the program is running. A constant is a value that is stored in a memory location which stays the same when the program is running. Variables are needed when there is a value that is dependent on the user running the program and constants are needed when there is a value that is the same every time the program is run no matter the user.

1 of 8

## Arithmetic, relational and logic operators in Trut

Arithmetic operators are used in mathematical equations and include: addition, subtraction, multiplication, division, indices and brackets. You can use BIDMAS to know what order the operators are used in.

Relational operators are how one piece of data relates to another. They include: greater than, less than, equal to, greater than or equal to and less than or equal to.

Logic operators are used to see if two numbers in binary code are either both 1, one is 1 or the opposite of it is 1. These logical operators are AND (both 1), OR (one or the other is 1) and NOT (the opposite of the input is 1)

2 of 8

## Selection and iteration

Selection constructs are used to provide an alternative pathway to a decision. IF statements are used when there is a decision; ELSE statements are used to offer an alternative output for the decision and ELSE IF statements are used to ask more than one question dependent on the answer to the first question.

Iteration constructs are used to create loops in the program so that the same instructions can be carried out a number of times. There are two main types of iteration - definite and indefinite. Definite iteration is used when the number of iterations is known in advance and can be called "count controlled" iteration. Indefinite iteration is where the number of iterations is not known in advance and can be called "condition controlled" iteration.

3 of 8

## Data types

There are four different types of data: Integer, Real, Boolean and Character.

Integers are whole numbers, for example 14.

Real numbers are numbers with decimal places, for example 32.1

Boolean is where there are only two possible answers to a question - usually yes and no

A character is a single letteror symbol for instance M

4 of 8

## Arrays

An array is variable with several different bits of information stored in it and all this information must be the same data type (e.g. real). The benefits of an array are that they increase the efficiency of the program due to the fact that they elimintae the need for a different variable for each bit of information. To create an array in pseudocode you use SET e.g. SET firstnames TO ["Alex" "Bryn" "Eloise" "Lois"] - the square brackets signify the start and end of the array.

5 of 8

## Sub Programs

Sub programs are self contained modules of code that performs a specific task and can be called upon by the main program at any point. Benefits of sub programs are: they reduce the complexity of a program and make the program easier to understand; they save time because the code only needs to be written once and they allow large teams to work together on a piece of code as they can each produce their own sub program in contribution. There are two different types of sub programs - functions and procedures. A function is a program that will return a value or outcome however a procedure is a program that will not return an output and will just carry out a series of actions.

6 of 8

## Validation

Validation is the process of checking if data inputted by a user is true so that the program can run correctly. There are lots of different validation checks:

Range check - checks data (normally numerical) is between two boundaries

Validating user input - makes sure what the user has typed is an option

Length check - used to ensure the right amount of digits/characters are used

Precense check - checking that the user has actually inputed something

Look up check - ensures the user can only choose from certain options

Menu - sets up options for the user to pick from

To test a validation check there are three different types of data that should be used - normal data (data that should be accepted by the program), boundary data (data that is at the outer limits of what should be accepted) and erroneous data (data that should not be accepted).

7 of 8

## Testing and test plans

There are three different errors that can occur in a program: 1) a syntax error - when something is wrong with the code itself e.g. a spelling mistake; 2) a logic error - where the code will run but something will cause an unexpected output e.g. a wrong relational operator and 3) a run time error - when the data inputted by the user cobtradicts the program e.g. a charcter instead of a number. To find and correct these errors, testing is used. In some software - like python - there is already an IDE (intergrated design environment). This is a software that helps programmers to develop code by flagging up syntax errors and issuing helpful messages. At the start of developing a program, the programmer will design a test plan - this will be a list of all the requirements of the program and how they will be tested. As the program is made, the tests outlined in the plan will be run throughout the project to ensure that the end program works correctly.

8 of 8