Inputs, Outputs & Assignments o Define variable, constant, assignment, casting, input, output, integer,
real, float, character and string.
o Variable - a value stored in memory that can change while the program is running. o What is the difference between a variable and a constant?
• The MAR contains the address of an instruction or data to be fetched or o Why are constants easier to use than variables?
written to memory. Similarly, a variable is a pointer to a memory address o Why is casting necessary in a program?
which we’re able to give a user-friendly label/name. o Why might integers be more useful than float in a program?
o Constant - a value that does not change while the program is running, and is
assigned when the program is designed.
• Makes programs easier to read as they’re usually declared and assigned
Three Programming Constructs
at the top of the program. o Sequence is executing one instruction after another.
• Can easily be changed by the programmer in one place in a program, o Selection is a program branching depending on a condition.
instead of changing every instance of a value throughout a program; leads
• If statement - if a statement is true, execute this line of code,
to less chance of errors.
otherwise, or else, execute this line of code instead.
• The compiler can optimise the code; this makes a program run more
• Some programming languages support switch/select… case
quickly if constants are used instead of variables.
statements, where a program can branch in several directions
o Assignment - giving a variable or a constant a value.
depending on the value of a variable with each “case” placed after a
o Casting - converting a variable from one data type to another (e.g. from an integer
single “switch”.
to a string); a variable can be an integer, character, string, real (float) or Boolean. o Iteration, sometimes called looping, is repeating sections of code.
• The ALU must use numbers for addition, so a string has to be "cast" to a
• For loops (known as 'counter-controlled loops') are used when the
number.
number of iterations needed is known ahead of the iteration
• Integers require less memory than float numbers; integers can make a
executing.
program more memory efficient. However, it may be necessary to cast an
• While loops (also known as 'condition-controlled loops') are used
integer to real in a program, and some commands may require a specific
when the number of iterations is not known because the variable
data type.
used to determine when the iteration ends is changing within the
o Input - a value that is read from an input device, like a keyboard.
iteration itself.
o Output - data generated by the computer and displayed to the user.
• Do… until loops are an alternative to while where the code
o Integer - a whole number; positive or negative; used only when arithmetic on the
executes at least once before the condition is checked.
data is required - e.g. 6 - Use: total = total + score – can’t be used for telephone
numbers.
o Real - number with a decimal part; sometimes called float/floating-point number Three Programming Constructs Questions
- e.g. 6.5 - Use: cost = total + vat. o What are the three programming constructs?
o Character - a single alphanumeric - used for menu choices - e.g., 1 or a - Use: o What is the difference between for, while, and do… until loops?
choice = input("Enter your choice:"). o What are the alternative names for 'for' and 'while' loops?
o String - multiple alphanumeric characters - used for words and telephone numbers o How do 'if' and 'switch/select… case' statements work? How are they
- e.g. Craig - Use: forename = Craig. different?
, Arithmetic and Comparison Operators Boolean Operators
o The Arithmetic Logic Unit (ALU) performs: arithmetic o There’re three Boolean operators: and, not and or.
calculations, logical comparisons and binary shifts. • And - execute code if both statements are true.
o Arithmetic Operators: • Not - execute code if one statement is true but not the other.
• Addition (+) • Or - execute code if one statement or the other statement is true.
• Subtraction (-) o An infinite loop can be created by executing 'while True'.
• Multiplication (*) o It’s called a Boolean expression because that expression equates or evaluates to either
• Division (/) true or false.
• Exponentiation (^) • 0 AND 1 = 0
• Modulus (MOD) – how many whole numbers are left • 0 OR 1 = 1
once you fit the number on the right into the number on • NOT 0 = 1
the left as many times as you can
• Integers Division (DIV) – how many whole times does Boolean Operators Questions
the number on the right fit into the number on the left
o Why is a Boolean expression called that?
o Comparison Operators:
o What are the three Boolean operators? How does
• Equal to (==)
• Not equal to (!=) each one work?
o How can you create an infinite loop?
• Less than (<)
• Less than or equal to (<=)
• Greater than (>) o Variables and constants can hold: integer, real (float), character, string and Boolean.
• Greater than or equal to (>=) o Each data type uses a different amount of memory; to optimise the program the correct
data type should be used.
o What is the purpose of the MOD and DIV arithmetic operators? • e.g. The number 22 should be stored as an integer rather than float, as it takes less
o What do the != , == , <= and >= comparison operators mean? memory and isn't a decimal - 22 takes up less than 22.0.
o Concatenating numbers stored as an integer requires conversion to the string data type,
as by remaining in the integer data type, the program will instead add the two numbers.
o What is casting? Why is it o It's easy to convert string data types back into integers (or other data types) so we can
necessary?
o What data types can variables Data Types o
perform arithmetic calculations, by simply putting "int" before the variable it is stored in.
We can ask the user to input numbers but as characters, even though we want them to be
and constants hold?
o How would you cast a variable and Casting o
stored as integers. Therefore, we can cast them (convert them) into integers.
Changing a data type from one to another is called casting.
to a string or an integer? Why o Casting is necessary so that sub-problems receive data in a format they are expecting.
might this be useful in a o Casting allows numbers to be manipulated as strings, and allows inputs that are always
program? string to become numbers.
o Why should telephone numbers • x = str(x) casts the variable x to a string.
be stored as strings and not • x = int(x) casts the variable x to an integer.
integers?