knowt logo

Unit 10.1 Introduction to Programming Theory

Aims

  • Know what is meant by the terms variable and constant.

  • Know what it meant by the term data type and the different data types commonly found in programming language.

  • Know what is meant by the term record structure.

  • Know techniques that can be used to represent sequence, selection and iteration in programming code.

  • Know the difference between procedures and functions.

  • Know why files are used in programming code.

  • Know what is meant by the term array and the difference between one-dimensional and two-dimensional arrays.

  • Be able to manipulate data in an array using a linear search and bubble sort.

Variables & Constants

Single data pieces reserved in computer memory that can be accessed by the programme and must be declared before use.

Variable’s value CAN be changed by the code

Constant’s value CANT be changed whilst programme is running.

Identifier Names

Variables & constants need unique names - have to accurately describe the contents to help users know what they represent and its easier to maintain code. e.g. num1 not n1

General Rules for naming Identifiers/procedures/functions

  • Sensible & related to data being stored

  • Starts with a letter (not a digit)

  • Contains only A-Z (both upper & lower) digits 0-9 and _ underscore.

  • Short & Concise

  • No spaces

  • Not a reserved word with special meaning in programming language i.e IF,ELSE

  • Follows suitable naming convention

Convention Name

Description

Example

Camel Case

1st letter of each word is capitalised except from 1st word, spaces are removed. (capital letters are camel humps)

firstName

Snake Case

All lower case and spaces replaced with _ (think_ as snake slithering on its belly)

first_name

Kebab Case

All lower case and spaces replaced with - (think- as kebab skewer)

first-name

Data Types

Specifies how data will be stored in code. Data type is required when setting up variable/constant.

Data Type

Meaning

Examples

CHAR

Store single letter/number/punctuation/symbol

A 9

STRING

Store combo of letters/numbers/punctuation/symbol

Mel 10 Main Road

INTEGER

Store whole numbers only

10 -10

REAL

Store numbers with decimal place

10.1 843.942

BOOLEAN

Store values that can only be True/False

TRUE FALSE

DATE

Store valid calendar dates

15/11/2024

ARRAY

Hold lists of the same data type

A, B, C 1,2,3

FILE

Store data in external file

Text Files

Record Structures

Allows the storage of multiple related items of different data types together.

Easier to think of it like a table - each record (row) holds details and each field (column) is given a data-type relating to the data stored in that field.

Programming Constructs

AKA control structure, determines flow of program, by saying which lines of code will be run and their order, three programming constructs are: Sequence, Selection and Iteration.

Sequence

Each line of code is run one after the other from 1st to last

Selection aka Branching

Program flow is interrupted and a condition is tested, depending on the outcome, control is passed to another bit of code. e.g. IF…ELSE (IF is 1st condition test, if true it runs one bit of code if false it goes to ELSE and runs that code.)

An alternative to if…else is CASE - tests variable against multiple possible values e.g.

Iteration aka Looping

One or more lines of code are run more than once:

  • Count-Controlled Loop - repeats section of code a fixed number of times e.g. for loop.

  • Condition-controlled Loop - repeats a section of code until condition is met e.g. Pre Condition (while loops) or post condition (repeat loops)

For Loops

Repeats section of code a fixed number of times,

While Loops (Pre-Condition Loop)

Tests a condition at start of loop, if its met then the loop wont run and vice versa (can also be Do…Until)

e.g. While Loop that outputs the word “Hello” until count is not less than or equal to 10

Repeat Loops (Post-condition Loop)

Tests a condition at end of loop, therefore code inside loop runs atleast once.

e.g. Repeat Loop that outputs word “Hello” until count is = 11

Procedures

Single block of code, when its called from another point in the program, it completes a task and then hands control back to the line of code that called it.

Can have parameters - variable/data piece that is passed into procedure to allow task completion.

When declaring a procedure, the procedure name must be stated parameters to be used and the code the procedure will carry out. e.g. procedure below for addition

Procedure’s DONT return values e.g. user enters 5 no’s sent to procedure, which sorts and then prints the numbers in Asc order, they are sorted on screen and NOT returned back to original line of code.

Functions

Similar to procedure - block of code called from another point of program to carry out task, once completed it hands control back to original line of code however functions WILL return a value!

Files

Whilst program is running, data is held inside a variable/constant/array or other data structure form.

Data only remains in the structure whilst program is running, if it stops the data is lost.

Common file type is text file - once data is written to that file, the program can close and data is still kept, when program is ran again, data is read from the file and then transferred back into the program.

Arrays

Variables store a single data piece in a computer program. e.g. num1 < 2, num2 < 5, etc.

Arrays are an alternative to storing every variable as a line of code - only a list of variables of the same data type and have same identifier name.

Declaring a new array needs the array name, no. of elements to be stored (upper & lower bounds) and data type to be stated.

One-Dimensional

Stores data in linear fashion in a single row, data is stored in consecutive locations.

e.g. Array called “letters” with 30 spaces of data type string. letters (1) is X

Two-Dimensional

Stores data in grid-like structure made up of rows and columns.

e.g. Array called noughtsAndCrosses - noughtsAndCrosses (3:2) is row 3, column 2 - O

Array Drawbacks

  • Static - when arrays are declared the size has to be stated to reserve enough memory space, this can’t be inc/dec while program is running.

  • Must be the same data type - Data type needs to be stated when array declared, once its been set no other data type can be used.

Searching an Array

To find a given value a linear search can be done: goes down the list examining each array item and compares it to search value, it searches until the item is found or the end of the array.

Sorting an Array

Once set up, arrays can be sorted by asc/desc this is done by using a bubble sort.

Bubble sorts compare first two adjacent items and swaps them if in the wrong order, it keeps repeating these steps til the array is completely in order (sometimes needs to go through the array more than once)

Unit 10.1 Introduction to Programming Theory

Aims

  • Know what is meant by the terms variable and constant.

  • Know what it meant by the term data type and the different data types commonly found in programming language.

  • Know what is meant by the term record structure.

  • Know techniques that can be used to represent sequence, selection and iteration in programming code.

  • Know the difference between procedures and functions.

  • Know why files are used in programming code.

  • Know what is meant by the term array and the difference between one-dimensional and two-dimensional arrays.

  • Be able to manipulate data in an array using a linear search and bubble sort.

Variables & Constants

Single data pieces reserved in computer memory that can be accessed by the programme and must be declared before use.

Variable’s value CAN be changed by the code

Constant’s value CANT be changed whilst programme is running.

Identifier Names

Variables & constants need unique names - have to accurately describe the contents to help users know what they represent and its easier to maintain code. e.g. num1 not n1

General Rules for naming Identifiers/procedures/functions

  • Sensible & related to data being stored

  • Starts with a letter (not a digit)

  • Contains only A-Z (both upper & lower) digits 0-9 and _ underscore.

  • Short & Concise

  • No spaces

  • Not a reserved word with special meaning in programming language i.e IF,ELSE

  • Follows suitable naming convention

Convention Name

Description

Example

Camel Case

1st letter of each word is capitalised except from 1st word, spaces are removed. (capital letters are camel humps)

firstName

Snake Case

All lower case and spaces replaced with _ (think_ as snake slithering on its belly)

first_name

Kebab Case

All lower case and spaces replaced with - (think- as kebab skewer)

first-name

Data Types

Specifies how data will be stored in code. Data type is required when setting up variable/constant.

Data Type

Meaning

Examples

CHAR

Store single letter/number/punctuation/symbol

A 9

STRING

Store combo of letters/numbers/punctuation/symbol

Mel 10 Main Road

INTEGER

Store whole numbers only

10 -10

REAL

Store numbers with decimal place

10.1 843.942

BOOLEAN

Store values that can only be True/False

TRUE FALSE

DATE

Store valid calendar dates

15/11/2024

ARRAY

Hold lists of the same data type

A, B, C 1,2,3

FILE

Store data in external file

Text Files

Record Structures

Allows the storage of multiple related items of different data types together.

Easier to think of it like a table - each record (row) holds details and each field (column) is given a data-type relating to the data stored in that field.

Programming Constructs

AKA control structure, determines flow of program, by saying which lines of code will be run and their order, three programming constructs are: Sequence, Selection and Iteration.

Sequence

Each line of code is run one after the other from 1st to last

Selection aka Branching

Program flow is interrupted and a condition is tested, depending on the outcome, control is passed to another bit of code. e.g. IF…ELSE (IF is 1st condition test, if true it runs one bit of code if false it goes to ELSE and runs that code.)

An alternative to if…else is CASE - tests variable against multiple possible values e.g.

Iteration aka Looping

One or more lines of code are run more than once:

  • Count-Controlled Loop - repeats section of code a fixed number of times e.g. for loop.

  • Condition-controlled Loop - repeats a section of code until condition is met e.g. Pre Condition (while loops) or post condition (repeat loops)

For Loops

Repeats section of code a fixed number of times,

While Loops (Pre-Condition Loop)

Tests a condition at start of loop, if its met then the loop wont run and vice versa (can also be Do…Until)

e.g. While Loop that outputs the word “Hello” until count is not less than or equal to 10

Repeat Loops (Post-condition Loop)

Tests a condition at end of loop, therefore code inside loop runs atleast once.

e.g. Repeat Loop that outputs word “Hello” until count is = 11

Procedures

Single block of code, when its called from another point in the program, it completes a task and then hands control back to the line of code that called it.

Can have parameters - variable/data piece that is passed into procedure to allow task completion.

When declaring a procedure, the procedure name must be stated parameters to be used and the code the procedure will carry out. e.g. procedure below for addition

Procedure’s DONT return values e.g. user enters 5 no’s sent to procedure, which sorts and then prints the numbers in Asc order, they are sorted on screen and NOT returned back to original line of code.

Functions

Similar to procedure - block of code called from another point of program to carry out task, once completed it hands control back to original line of code however functions WILL return a value!

Files

Whilst program is running, data is held inside a variable/constant/array or other data structure form.

Data only remains in the structure whilst program is running, if it stops the data is lost.

Common file type is text file - once data is written to that file, the program can close and data is still kept, when program is ran again, data is read from the file and then transferred back into the program.

Arrays

Variables store a single data piece in a computer program. e.g. num1 < 2, num2 < 5, etc.

Arrays are an alternative to storing every variable as a line of code - only a list of variables of the same data type and have same identifier name.

Declaring a new array needs the array name, no. of elements to be stored (upper & lower bounds) and data type to be stated.

One-Dimensional

Stores data in linear fashion in a single row, data is stored in consecutive locations.

e.g. Array called “letters” with 30 spaces of data type string. letters (1) is X

Two-Dimensional

Stores data in grid-like structure made up of rows and columns.

e.g. Array called noughtsAndCrosses - noughtsAndCrosses (3:2) is row 3, column 2 - O

Array Drawbacks

  • Static - when arrays are declared the size has to be stated to reserve enough memory space, this can’t be inc/dec while program is running.

  • Must be the same data type - Data type needs to be stated when array declared, once its been set no other data type can be used.

Searching an Array

To find a given value a linear search can be done: goes down the list examining each array item and compares it to search value, it searches until the item is found or the end of the array.

Sorting an Array

Once set up, arrays can be sorted by asc/desc this is done by using a bubble sort.

Bubble sorts compare first two adjacent items and swaps them if in the wrong order, it keeps repeating these steps til the array is completely in order (sometimes needs to go through the array more than once)

robot