Wednesday 8 February 2017

1.3 Basic operations in programming

  • Variable: a data item whose value will change during the execution of the program
  • Arithmetic operation: common expressions such as +, -, /, x
  • Rounding: reducing the number of digits used to represent a number while maintaining a value that is approximately equivalent
  • Truncating: the process of cutting off a number after a certain number of characters or decimal places
  • Random number generation: a function that produces a completely random number
  • Pseudo-random number generator: common in programming languages, a function that produces a random that is not 100% random
  • Relational operations: expressions that compare two values such as equal to or greater than
  • Boolean operators: expressions that result in a TRUE or FALSE value
  • AND: boolean operation that outputs true if both inputs are true
  • OR: Boolean operation that outputs true if either of its inputs are true
  • NOT: Boolean operation that inverts the result so true becomes false and false becomes true
  • XOR: Boolean operation that is true if either input is true but not if both inputs are true
  • String-handling functions: actions that can be carried out on sequences of characters
  • Character code: a binary representation of a particular letter, number or special character

Arithmetic operations
  • Addition
  • Subtraction
  • Multiplication
  • Division of real numbers: A real number is one with a fractional part
  • Example: 3.1 = 6.2 / 2
  • Division of integers: An integer is a whole number and therefore may generate a number with a remainder
  • Example: 3r1 = 7 / 2
  • Modulo operation: MOD operation is used to divide one number by another to find the remainder
  • Exponentiation
  • Rounding
  • Truncating: Shortening a value by cutting it off after a certain number of digits
  • It is the equivalent of rounding down
  • Random number generator: creating a number to be used in a program that is random
  • Random numbers are very useful
  • Typical applications include:
    • A range of test data
    • Computer simulations
    • Creating random events and movements in computer games
    • Selecting a random sample from a dataset
  • Most random number generation techniques used in programming languages start from a seed value and then use an algorithm to create the random number, it means that the number cannot be truly random as the algorithm used will produce an element of structure to the results
  • Random numbers generated by programming languages are often referred to as pseudo-random numbers
  • In other circumstances, such as encryption, this level of randomness would not be sufficient

Relational operations
  • Relational operations work by making comparisons between two data items
  • Operands are the values and the operator is the comparison being made

Boolean operations
  • Fundamental to the process of searching data whether that is in a database, or on the web
  • The four basic operations are:
    • AND: known as conjunction
    • OR: known as disjunction
    • NOT: known as a negation
    • XOR: known as exclusive OR
  • It is possible to embed relational operations within boolean operations

String-handling functions

  • Length: there is usually an upper limit places on its size
  • You can set the maximum length or calculate the length of a particular string of data
  • Position: find the start position of a particular string of characters within another string
  • Substring: Various techniques can be used to extract data from anywhere in a string to create a substring providing the start and end position are known or the start position and length are known
  • Concatenation: adding strings together
  • Character codes: convert a text value to a numeric value i order to carry out a calculation on it, especially when encrypting data
  • String to integer / integer to string
  • String to float / float to string: a float is also called a real number
  • String to datetime / datetime string

1.2 Programming Concepts

  • Syntax: the rules of how words are used within a given language
  • Selection: the principle of choosing what action to take based on certain criteria
  • Nesting: placing one set of instructions within another set of instructions
  • Iteration: the principle of repeating process
  • Definite iteration: a process that repeats a set number of times
  • Indefinite iteration: a process that repeats until a certain condition is met
  • Loop: a repeated process
  • Sequence: the principle of putting the correct instructions in the right order within a program

Selection
  • The selection process allows a computer to compare values and then decide what course of action to take

Selection - Nested selection
  • This can be written as a case statement

Repetition (Iteration)

  • An iterative process has two parts - a pair of commands that show the start and finish of the process to be repeated and some sort of condition
  • Definite iteration means that the instructions are repeated in a loop a certain number of times
  • Indefinite iteration means that the instructions are repeated in a loop until some other event stops it

1.1 Programming Basics

  • Memory: the location where instructions and data are stored on the computer
  • Algorithm: a sequence of steps that can be followed to complete task and that always terminates
  • Syntax: the rules of how words are used within a given language
  • Memory address: a specific location where instructions or data are stored
  • Assignment: the process of giving a value to a variable or constant
  • Constant: an item of data whose value does not change
  • Variable: an item of data whose value could change while the program is being run
  • Debug: the process of finding and correcting errors in programs
  • Declaration: the process of defining variables and constants in terms of their names and data types
  • Data type: determines what sort of data are being stored and how it will be handled by the program
  • Integer: any whole positive or negative number including zero
  • Pointer: a data item that identifies a particular element in a data structure - normally the front or rear
  • Array: a set of related data items stored under a single identifier. Can work on one or more dimensions
  • Element: a single value within a set or list - also called a member
  • Record: one line of a text file


Naming and Storing Data
  • The data are stored in memory along with the instructions
  • Using meaningful names will help you when they are trying to trace bugs and it also allows other programmers to follow the code more easily
  • The process of giving data values is called ‘assignment’
  • A simplified visualisation of how data is handled
Memory Addresses
1000
1001
1002
Variable
Number1
Number2
Answer
Data
2
3
5


  • There will be millions of memory addresses, only three are shown in this diagram


Constants and Variables
  • Data are stored either as a constant or a variable
  • The name given to a constant should be self-explanatory
  • Meaningful names make the code easier to work with as the program gets bigger
  • It also makes it easier for anyone else that looks at the code, to work out what the program is doing
  • It makes it easier to debug
  • Several programmers can work on it at the same time
  • Easier to update the code


Data types
  • Integer - VB can store -2 147 483 648 to + 2 147 483 647
  • Real/float
  • Text/string
  • Boolean
  • Character
  • Date/Time
  • Pointer/reference
  • This data type is used to store a value that will point to or reference a location in the memory of the computer
  • If you think of memory as a series of pigeon-holes or addresses where instructions and data are stored, the pointer/reference is used in a program to go to a specific address
  • Array
  • Each individual name in the array is called an element
  • Records
  • Used to store a collection of related data items, where the items all have different data types


Built-in and user-defined data types

  • User-defined data types are combining existing data types together