2.2 - Programming Fundamentals (Part 1)

The use of variables, constants, operators, inputs, outputs and assignments

Variables

- A variable is a storage location identified by a memory address and an associated symbolic name (an identifier), which contains some known or unknown quantity of information referred to as a value.
- For example, a variable named 'cat' could store the value 'meow', and then you can access 'cat' to retrieve 'meow' later on in the code.

Assignments

- Assignments are the process of storing a value in a variable or constant. This is usually done using the equals sign (=).
- In programming languages, cat = "meow" is an assignment, and cat then contains "meow".

Constants

- A constant is a value that cannot be altered by the program during normal execution, i.e., the value is constant.
- It works the same as a variable, but it cannot be changed.
- While both OCR Exam Reference Language and Python assign variables with variable = value, Python uses CONSTANT = value for constants but OCR Exam Reference Language uses const constant = value.

Operators

- Operators are symbols that tell the compiler or interpreter to perform specific mathematical, relational, or logical operations and produce a final result.
- There are many operators:

Arithmetic Operators

Operation Python Operator OCR Reference Language Operator Example (Python) Example (OCR Reference Language)
Addition + + 5 + 3 5 + 3
Subtraction - - 5 - 3 5 - 3
Multiplication * * 5 * 3 5 * 3
Division / / 5 / 2 5 / 2
Quotient (division w/o remainder) // DIV 5 // 2 5 DIV 2
Modulus (division remainder only) % MOD 5 % 2 5 MOD 2
Exponentiation (power) ** ^ 5 ** 3 5 ^ 3

Comparison Operators

Operation Operator (same across both) Example (same across both)
Equal to == 5 == 3
Not equal to != 5 != 3
Greater than > 5 > 3
Less than < 5 < 3
Greater than or equal to >= 5 >= 3
Less than or equal to <= 5 <= 3

Inputs

- Inputs are the data or signals received by the system from the external environment, typically from a user or another system.
- Programs are useless without inputs because they can't interact with the outside world.

Outputs

- Outputs are the data or signals sent from the system to the external environment, typically to a user or another system.
- Programs are useless without outputs for the same reason.


The Three Basic Programming Constructs

- The three basic programming constructs used to control the flow of a program are:

Sequence

- Sequence is the order in which the instructions are executed.
- It's generally top-to-bottom line-by-line.
name = input("What is your name? ") // this gets executed first
print(f"Hello + name + "!") // this gets executed second

Selection

- Selection is when the flow of the program is changed based on a condition.
- Selection is best visualised with if/else statements.

name = input("What is your name? ")
if name == "John"
    print("Hello John!")
else
    print("Hello Not John!")
endif
            
- There are also switch/case (or match/case) statements used when you want to compare a value against multiple options.
- They're generally easier and more efficient when you have a lot of options to compare against, but if statements are more flexible (you can't write if word.length > 5 in a switch/case statement).
name = input("What is your name? ")
switch name:
    case "John":
        print("Hello John!")
    case "Jane":
        print("Hello Jayne!")
    default:
        print("Hello Not John or Jayne!")
endswitch
            

Iteration

- Iteration is when the flow of the program is repeated multiple times.
- It looks for a condition to be true, and if it is, it executes the code inside the loop.
- Iteration can be done with for or while loops.

for i = 1 to 10
    print(i)
next i
            
- This will print the numbers 1 to 10.
while i < 10
    print(i)
    i = i + 1
endwhile
            
- This will also print the numbers 1 to 10, but it works differently.
- The for loop is saying 'do this 10 times', and the second is saying 'do this while i is less than 10' (and then i is incremented by 1 inside the while loop).
- while true will always create an infinite loop, because true is always, well, true.

Data Types

Basic Data Types

- Integer: A whole number, used for counting or indexing.
- Example: 10, -4
- Converted to in both OCR Exam Reference Language and Python using int()

- Real (Float): A number with a decimal point, used for measurements and calculations.
- Example: 3.14, -2.5
- Converted to in both OCR Exam Reference Language and Python using float()

- Boolean: Represents two values: true or false, useful in decision-making.
- A 0 is false, and a 1 is true in binary.
- Example: true, false

- Character and String: A character is a single letter, symbol, or digit. A string is a sequence of characters.
- Example (Character): 'A', '9'
- Example (String): "Hello", "123abc"
- Converted to using str() and chr()

Data Type Operations

- Casting: Converting one data type to another, such as changing a string to an integer or vice versa.
- Example: int("5"), str(5)

- Choosing Suitable Data Types: Choose a data type based on the nature of the data. For example, use an integer for whole numbers, a float for decimals, and a string for text. Boolean is ideal for binary choices.