Nothing Special   »   [go: up one dir, main page]

1.1. Programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

AQA Computer Science AS-Level

3.1.1 Programming
Intermediate Notes

www.pmt.education
Specification:

3.1.1.1 Data types:


Understand the concept of a data type.
Understand and use the following appropriately:
● integer
● real/float
● Boolean
● character
● string
● date/time
● pointer/reference
● records (or equivalent)
● arrays (or equivalent)
Define and use user-defined data types based on language-defined
(built-in) data types.

3.1.1.2 Programming concepts:


Use, understand and know how the following statement types can be
combined in programs:
● variable declaration
● constant declaration
● assignment
● iteration
● selection
● subroutine (procedure / function)
Use definite and indefinite iteration, including indefinite iteration with the
condition(s) at the start or the end of the iterative structure. A theoretical
understanding of condition(s) at either end of an iterative structure is required,
regardless of whether they are supported by the language being used.
Use nested selection and nested iteration structures.
Use meaningful identifier names and know why it is important to use
them

www.pmt.education
3.1.1.3 Arithmetic operations
Be familiar with and be able to use:
● addition
● subtraction
● multiplication
● real/float division
● integer division, including remainders
● exponentiation
● rounding
● truncation

3.1.1.4 Relational operations in a programming language


Be familiar with and be able to use:
● equal to
● not equal to
● less than
● greater than
● less than or equal to
● greater than or equal to

3.1.1.5 Boolean operations in a programming language


Be familiar with and be able to use:
● NOT
● AND
● OR
● XOR

3.1.1.6 Constants and variables in a programming language


Be able to explain the differences between a variable and a constant.
Be able to explain the advantages of using named constants.

www.pmt.education
3.1.1.7 String-handling operations in a programming language
Be familiar with and be able to use:
● length
● position
● substring
● concatenation
● character → character code
● character code → character
● string conversion operations

3.1.1.8 Random number generation in a programming language


Be familiar with, and be able to use, random number generation.

3.1.1.9 Exception handling


Be familiar with the concept of exception handling.
Know how to use exception handling in a programming language with
which students are familiar.

3.1.1.10 Subroutines (procedures/functions)


Be familiar with subroutines and their uses.
Know that a subroutine is a named ‘out of line’ block of code that may
be executed (called) by simply writing its name in a program statement.
Be able to explain the advantages of using subroutines in programs.

3.1.1.11 Parameters of subroutines


Be able to describe the use of parameters to pass data within
programs.
Be able to use subroutines with interfaces.

3.1.1.12 Returning a value/values from a subroutine


Be able to use subroutines that return values to the calling routine.

www.pmt.education
3.1.1.13 Local variables in subroutines
Know that subroutines may declare their own variables, called local
variables, and that local variables:
● exist only while the subroutine is executing
● are accessible only within the subroutine
Be able to use local variables and explain why it is good practice to do
so.

3.1.1.14 Global variables in a programming language


Be able to contrast local variables with global variables.

www.pmt.education
Data Types

The way in which data is stored depends on what the data is. A ​data type​ is defined by the
values it can take​ or the​ operations which can be performed on it​.

Data type Description

Integer A whole number, positive or negative, including zero.

Real / Float A positive or negative number which can have a


fractional part.

Boolean A value which is either true or false.

Character A single number, letter or symbol.

String A collection of characters.

Data / Time A way of storing a point in time, many different formats


are used.

Pointer / Reference A way of storing memory addresses.

Records A collection of fields. You can think of a record as a row


from a table.

Arrays An indexed set of elements each of which has the same


data type.

www.pmt.education
User-defined data types

User-defined data types​ are based on ​existing data


types​ and used to create a ​customised data structure​.

For example, a shop might use a user-defined data


type called ​Customer​to store information about their
customers. The user-defined data type might have
attributes ​like ​Forename​
, ​Surname​and
EmailAddress​ .

The way in which you use user-defined data types ​differs between programming
languages​. It’s important that you know how to use them in your chosen language.

Programming Concepts

Programming languages support a ​variety of different statement types​, some of which are
explained in the table below.

Statement type Description

Variable declaration Creating a variable for the first time, giving it a ​name
and sometimes a ​data type​.

Constant declaration The same as variable declaration, but when creating a


constant​. The value of a constant ​does not change
while the program is running.

Assignment Giving a constant or variable a value.

Iteration Repeating ​an instruction, this could be ​definite ​or


indefinite ​(see below).

Selection Comparing values​ and ​choosing an action​ based on


those values.

Subroutine A named ​block of code​ containing a ​set of instructions


designed to perform a ​frequently used​ operation.

www.pmt.education
Definite and indefinite iteration
Iteration is the process of ​repeating a block of code​. Examples of iteration include ​for
loops and ​while​loops.

Definite iteration is a type of iteration in which the ​number of repetitions​ required is ​known
before the loop starts. Indefinite iteration is used when the number of repetitions required
is ​not known​ before the loop starts.

FOR Count ← 0 TO 63 WHILE Temperature = 18


OUTPUT Count Temperature = GetTemp()
ENDFOR ENDWHILE

This is an example of ​definite ​iteration. The The ​while​loop above uses ​indefinite
for​loop will run​ 64 times​ before finishing. iteration. The number of repetitions is ​not
known​ before the loop begins.

Nested Structures
Selection structures and iteration structures can be ​nested​.

This means that one structure is ​placed within another​ and


can easily be identified by different levels of ​indentation ​in
code.

For example, the pseudocode below consists of an ​if


structure, within which are further selection and iteration
structures.

Whenever a new IF Colour = “RED” THEN


selection or iteration WHILE Colour = “RED”
structure begins, the Colour ← UpdateColour()
code moves to a ENDWHILE
higher level of ELSE
indentation​. IF Colour = “GREEN” THEN
WHILE Colour = “GREEN”
Colour ← UpdateColour()
ENDWHILE
ELSE
Colour ← “RED”
ENDIF
ENDIF

www.pmt.education
Meaningful Identifier Names
When declaring a variable, it’s important to give it a ​sensible ​and ​meaningful​ identifier
name. This makes it ​easier for others to understand​ what the purpose of the named object
is within the program.

If a different programmer, who was ​unfamiliar ​with your program, were to read the code,
they should be able to work out the purpose of a constant, variable or subroutine from its
name.

Arithmetic Operations

The following operations can be applied to values by your programming language.


Different languages notate these operations differently, so ensure that you’re familiar with
your chosen language’s approach.

Operation Description Example

Addition Adding together two numbers. 128 + 42 = 170

Subtraction Taking one number away from another. 34 - 13 = 21

Multiplication Timesing two numbers together. 64 * 2 = 128

Real / Float Dividing one number by another. 12 / 8 = 1.5


Division

Integer Division The same as real / float division, but just the 12 \ 8 = 1
whole number part​ is given. Or​ 12 DIV 8 = 1

Modulo Returns the remainder of an integer 12 MOD 8 = 4


division.

Exponentiation Raising one value to the power of another. 2 ^ 6 = 64

Rounding Limiting the​ degree of accuracy​ of a 3.14159 = 3.14


number. to 3 significant figures

Truncation Removing the decimal part​ of a number. 3.14159 truncated = 3

www.pmt.education
Relational Operations

You can make use of relational operators whenever you need to compare two values.
They are used in ​if​statements and ​while​loops to name a few examples.

Operation Example

Equal to 5=5

Not equal to 16 <> 54


16 != 54

Less than 75 < 76

Greater than 19 > 18

Less than or equal to 6 >= 7


8 >= 8

Greater than or equal to 5 >= 4


6 >= 6

Boolean Operations

As explained earlier, a Boolean data type is one with a value that can ​only ever be true or
false​. There are a series of ​operations ​that can be performed on Boolean values.

Operation Description Example

NOT The ​opposite ​of a NOT 1 = 0


Boolean value

AND Two Boolean values 1 AND 1 = 1


multiplied ​together 0 AND 1 = 0

OR Two Boolean values 1 OR 0 = 1


added ​together 1 OR 1 = 1

XOR True if ​exactly one​ of two 1 XOR 1 = 0


values is true 1 XOR 0 = 1

www.pmt.education
Constants and Variables

When a program needs to store data, it usually does so using one of two types of data
item: ​constants ​or ​variables​.

As their name suggests, variables can ​change their value​ during the execution of a
program, whereas a constant’s value ​cannot change​ once assigned.

Constants can be used for storing data that ​doesn’t need to


change​ such as a value for ​pi​ or the number of days in a
year. Using constants allows values to be given ​identifier
names ​which makes code ​easier for a human to understand​.

Using a constant makes changing a value ​much easier ​as it


only needs to be updated ​in one place ​in the code.

Using hard-coded values Using constants

HoursWorked ← USERINPUT HourlyRate ← 14


PAY ← 14 * HoursWorked HoursWorked ← USERINPUT
OUTPUT PAY PAY ← HourlyRate * HoursWorked
OUTPUT PAY

The pseudocode examples above show two different approaches to the same problem.
One approach uses hard-coded values whereas the other uses constants.

The code which makes use of constants is ​easier to understand​ as it clearly specifies that
14​refers to an hourly rate. In the example which uses hard-coded values, it’s ​difficult to
understand ​why ​HoursWorked​is being multiplied by 14.

www.pmt.education
String-handling operations

Strings can have ​various functions​ applied to them.

Function Description

Length Returns the ​number of characters​ in a specified string.

Position Returns the ​position of a specified character ​within a string.

Substring Given a starting position and a length, returns a ​portion of a


string​.

Concatenation Joining two or more strings together​ to form a new, longer


string.

Character to character Returning the ​character code​ which corresponds to a


code specified character.

Character code to Returning the ​character ​represented by a given character


character code.

String to integer Converting a string to an integer.

String to float Converting a string to a float.

Integer to string Converting an integer to a string.

Float to string Converting a float to a string.

Date / time to string Converting a date / time data type to a string.

String to date / time Converting a string to a date / time data type.

www.pmt.education
Random number generation

Most programming languages have the ability to ​generate random numbers​.

A built-in function takes a ​seed value​ and uses a series of ​mathematical operations​ to
arrive at a number.

It’s important that you make yourself familiar with random number generation in your
chosen programming language.

Exception handling

When an error occurs in program code, an “​exception​” is said to be thrown.

Once an exception has been thrown, the computer has to ​handle the exception​ to avoid
crashing. It does this by ​pausing execution​ of the program and saving the current state of
the program before running a section of code called a ​catch block​.

This code will ​prevent the program from crashing ​and might ​inform the user​ that an error
has occurred. Once the exception has been handled, the program restores its previous
state before resuming execution.

Subroutines

A subroutine is a ​named block of code​ containing a ​set of


instructions​ designed to perform a ​frequently used​ operation.
Using subroutines ​reduces repetition​ of code and hence
makes code ​more compact​ and ​easier to read​.

Both ​functions ​and ​procedures ​are types of subroutine and


can be ​called by writing their name​ in a program statement.
While both functions and procedures can return a value,
functions are ​required to​ whereas ​procedures may not​.

www.pmt.education
Parameters of subroutines

Parameters are used to ​pass data​ between subroutines within programs. Specified ​within
brackets​ after a subroutine call, parameters hold ​pieces of information​ that the subroutine
requires to run.

Length ← USERINPUT
Width ← USERINPUT
OUTPUT CalculateArea(Length, Width)

SUBROUTINE CalcualteArea(x, y)
RETURN x * y
ENDSUBROUTINE

The subroutine ​CalculareArea​in the pseudocode above takes two parameters,


Length​and ​Width​ . It then returns the product of the two values.

Returning values from a subroutine

A subroutine can return a value. One that ​always ​returns a value is called a ​function​, but
don’t think that procedures can’t return a value, they can (but don’t always).

Subroutines that return values can ​appear in expressions​ and be ​assigned to a variable or
parameter​.

Length ← USERINPUT
Width ← USERINPUT
Area ← CalculateArea(Length, Width)
OUTPUT Area

SUBROUTINE CalcualteArea(x, y)
RETURN x * y
ENDSUBROUTINE

For example, in the pseudocode above, the variable ​Area​is ​assigned ​to the subroutine
CalculateArea​ . The value taken by the variable will be the value returned by the
subroutine.

www.pmt.education
Local variables in subroutines

A local variable is a variable that can ​only be accessed from the subroutine within which it
is declared​. They only exist in the computer’s memory when their parent subroutine is
executing. This makes local variables a ​more memory efficient​ way of storing data than
using global variables, which are discussed below.

Global variables

In contrast to local variables, global variables can be​ accessed from any part​ of a program
and exist in memory for ​the entire duration​ of the program’s execution.

www.pmt.education

You might also like