Programming Fundamentals - OCR GCSE
Variables and Constants
Data in programs is referred to as values. A variable is a named memory address that holds a value that can be changed and reassigned. A constant is the same but it’s value cannot be changed.
A variable’s name is called an identifier. Common identifier formatting rules: ● Must start with letter
● Must contain at least 1 letter
● Mustn’t contain special characters. Eg. £
● Mustn’t contain spaces (use underscores instead)
● Should be lowercase, if it is multiple words the first letter of each word except from the first word should be in capitals.
● Name should represent value it is holding
Declaration and Assignment
A variable must be identified before it can have a value assigned to it. Giving a variable a value is known as an assignment, once a variable has been assigned a value then it can be used. In some programming languages (eg. python) the variable can be declared and assigned on the same line.
Constants
A constant is similar to a variable but its value cannot be changed once it is assigned. Used for values that will not usually change. Eg. Pi = 3.142
Global and Local Variables
A global variable can be accessed throughout the whole program whereas a local variable can only be accessed in the subroutine/subprogram it was declared in. If a local variable has the same name as a global variable then the value of the global variable will be changed.
The Three Programming Constructs
There are three main constructs (building blocks) that form the basis of all programs, these are:
● Sequence - The order instructions occur and are processed
● Selection - The path the program takes when running
● Iteration - The repeated execution of a section of code when the program is running. Iteration is either count-controlled or condition-controlled.
Sequence (Construct 1)
The order statements are executed. If the statements are executed in the wrong order it can lead to errors.
Selection (Construct 2)
The process of making a decision, decides which path the program takes. Gives a boolean result. (True or False). Implemented using ‘if then else’ statements.
Iteration (Construct 3)
Repeating a section of code until a certain condition is met. Often referred to as looping. Make the program much simpler and shorter.
● Count-controlled iteration: Repeats a section of code a predetermined number of times. Uses ‘for and ‘next’. Everytime the section loops, the count is increased by one. Once count reaches the required number the result turns from False to True and the iteration ends.
● Condition-controlled iteration: Repeats the section of the code until a condition is met or no longer met. Can be either while loop (uses statements ‘while’ and ‘endwhile’) or repeat loops (uses statements ‘repeat’ and ‘until’)
While condition controlled loop: Tests condition at start of loop, if it is met/not met then loop is executed and program loops back to start and checks condition again. ‘While’ statement defines start and ‘endwhile’ defines end. Code may never execute. Repeat condition controlled loop: Similar to while condition controlled loops but the condition is tested at the end of the loop instead of the start. ‘Repeat’ defines start and ‘until’ defines end. Code in loop is always executed at least once as conditions are tested at the end.
Infinite Loops
Condition controlled loops may run forever and cause an infinite loop. (where the program runs indefinitely). Can be created using either ‘while’ or ‘repeat’ loops.
Nesting
Occurs when one construct is included within another. Reduces the amount of code needed. Easier to debug and edit code.
● Nested selection: Can increase the number of possible paths at a decision point. Allows complex decisions to be made.
● Nested iteration: Allows iterations to be nested, eg. two count controlled loops can be nested and can output the times table.
Common Arithmetic Operators
An operator determines what action is performed. There are three types of operators:
● Mathematical operators: Designed to carry out calculations and allows arithmetic to be performed on values. Eg. ‘addition’ or ‘subtraction’
● Comparison Operators: Allows comparisons to be made. Used in condition testing. Eg. ‘less than’ or ‘assignment’
● Logical operators: Used to make more complex decisions by combining logical operations. Eg. ‘And’ or ‘Not’
Applying Arithmetic Operators
● Modulus gives remainder from division
● Integer division discards remainder
● Brackets are used to show which operations to perform first