Chapter 2: Programming
2.1 Develop Code
- A program is an algorithm that has been converted into program code so that it can be executed by a computer. A well-written algorithm should be free of logical errors and easy to code in any high-level language.
- Syntax error: An error that occurs when a rule of the programming language is broken.
- When algorithms are converted into programs the computer needs to be told what type of data is stored in each variable. Every programming language has a number of built-in data types.
- Data type: Specifies what kind of data it can hold. Common data types are integer, real, Boolean and character. The data type of value determines the operations that can be performed upon it.
- Variable initialisation:
- When a variable is declared, the computer allocates it a location in memory. Initially, this location is empty, so before a variable can be used it has to be given a value.
- Once a variable has been initialised an assignment statement is used to change its value.
- Assignment statement: The SET….TO command is used to initialise variables in pseudocode.
- Type coercion: The data type variable gets changed during program execution. The process of converting the value stored in a variable from one data type to another.
- Command sequence: A set of instructions that the computer executes one after another in order. Usually combined with loops ad selection statements.
- Selection: Used to create a branch in a program. The computer selects which branch to follow based on the outcome of a condition.
- Loops: Another name for an iteration. Used to make a computer repeat a set of instructions more than once.
- The four basic data types are integer, float/real, Boolean and character.
- Variable and type decelerations, command sequences, selection and iteration are four of the structural components of a program.
2.2 Making programs easy to read
- Techniques used to make programs easy to read and understand:
Technique
Description
Comments
Used to explain what each part of the program does.
Descriptive names
Using descriptive identifiers for variables, constants and subprograms help make their purpose clear.
Indentation
Makes it easier to see where each block of code starts and finishes. Getting the indentation wrong can result in the program not running.
White space
Adding blank lines between different blocks of code makes them stand out.
-
- Proving readable code makes it easier to understand what a program does and how it does it.
2.3 Strings
- String: A sequence of characters. They can be letters, numbers, symbols, punctuation marks or spaces.
- String indexing: Each character in a string has an index number, with the first character at position 0. You can use the index to reference individual characters in a string.
- Length:
- LENGTH function used to find the number of characters in a string.
- Function: A subprogram that performs a specific task and can be used at any point in the program.
- String traversal: Using a loop to cycle through each character in a string. Can use a FOR loop.
- Concatenation: Involves joining two or more items of information together, Concatenating two strings produces a new string object.
- Type conversion: Performed when a string and a non-string are joined together. The computer converts the non-string into a string before joining the two strings together.
- Slicing: The process of extracting part of a string.
- String formatting: Used to control the way text is displayed on the screen.
2.4 Data Structures
- Data structure: An organised collection of related elements. Arrays and records are two common data structures used in programming.
- One-dimensional arrays: A list of elements, each of which has a unique index value representing its position in the list.
- Two-dimensional arrays: A matrix of rows and columns resembling a table. Two indices are used, one to reference the rows and the other the columns. All the elements of a two-dimensional array share the same data type.
- All the elements in an array have the same data type.
- A record consists of a collection of fields. The values stored in a record can be of different data types.
2.5 Input/Output
- Validation: To check that the data entered by a user or from a file meets specified requirements. Any program that requires data entry should have appropriate forms of validation built-in.
- Range check: Used to ensure that data is within a specified range.
- Length check: Used to ensure that data has a length within a specified range.
- Presence check: Used to ensure the user has entered some data.
- Look-up check: Used to ensure the data matches one of the values in a predefined list.
- Who user are required to choose from a list of options, their input should be validated to ensure that their choice is valid.
- Large sets of data are normally stored in text files. The advantage of writing data to a file is that the data is not lost when the program is terminated. It can be read format he file whenever it is needed.
2.6 Subprograms
- Subprogram:
- A self-contained module of code that performs a specific task.
- Using subprograms reduces the complexity of programs and makes them easier to understand.
- There are two types of subprograms : functions and procedures.
- Procedures: A subprograms contains a set of statements that are executed when the procedure is called. Unlike a function, a procedure does not return a value to the main program.
- Functions: A subprogram that returns a value to the main program.
- Abstraction: The process of removing or hiding unnecessary detail so that only the important points remain.
- The two variables that store the random number generated by the function:
- Local variable: Can be accessed only from within the subprogram in which it is created.
- Global variable: Can be accessed from anywhere in the program, including inside subprograms.
- Built-in functions: Functions that are provided in most high-level programming languages to perform common tasks.
- Procedure: A subprogram that does not return a value to the main program.
- Parameters: Values that are passed to a subprogram when it is called.
2.7 Testing and evaluation
- Testing: This is the only way ensuring that the program functions correctly and meets all of the specified requirements.
- Evaluating programs: Need to able to identify the strengths and weaknesses of the programs as well as those created by other programmers and identifying areas for improvement.1
- Runtime errors: An error that occurs while the program is running - the operation the computer is asked to do is impossible to execute.
- Logic errors occur when there is an error in the logic of the code, causing the program to produce an unexpected result.
- Syntax error: Occurs when part of the code breaks the rules of the programming language.
- Runtime error: Occurs while the program is running and it is asked to do something that is impossible.
- Truth tables: Can be used to manually trace the execution of an algorithm, allowing you to track the changes in variable values.