3.2.3 Arithmetic, Relational and Boolean Operations in a Programming Language

Table of Contents

Fork me on GitHub

1 Arithmetic Operations

Arithmetic_Operators2.png

Learn It: What are Arithmetic Operations?

Operations – In the context of Computer Science, an operation is an action that
is performed on one or more pieces of data in order to produce additional data.
There are Arithmetic, Relational and Boolean Operations.
Arithmetic Operations - A process performed on one or more integer and real data
values. Examples of arithmetic operators are: + - * ^ / // %.
  • Operations are things that you can do to specific types of data.
  • For example, you can perform mathematical operations on numbers, string handling operations on text and logical comparisons on Boolean expressions.
  • The acronym BIDMAS is used to remember the correct order of operations (Brackets, Indices, Division or Multiplication, Addition or Subtraction).
  • You should be familiar with some of Pythons basic operators from the previous topics and the table below details all of the arithmetic operators we will use in Python:

Arithmetic_Operators_Table.png

  • Notice how the first four symbols are the same for pseudocode and Python.
  • It is only the last two options which change from the words (“DIV” and “MOD”) using in pseudocode to the symbols (“//” and “%”) used in Python.

Learn It: Assignment Statements

  • Assignment Operations – In most programming languages, values are assigned to variables and functions using an = sign.
  • In programming, a common mistake is to get the assignment operator = and the comparison operator == mixed up.
  • The following examples show the correct use of the assignment operator:
x = 1
PI = 3.142
alpha = "a"
street = "Elm Street"
over18 = True
  • In pseudocode, we use the AQA pseudocode standard guide which can be found on the link above the main topics.
  • In pseudocode we will use the ← symbol to mean assignment, e.g. x ← 1

  • When performing operations on data items, you need to consider the data types used.
  • For example, in a simple calculation where two whole numbers are added together, variables could be defined as follows:
    • Num1 = 7
    • Num2 = 10
    • total = 0
  • But if the calculation involves division, then the answer variable should be declared as a real (float) number because the result is unlikely to be a whole number:
    • Num1 = 7
    • Num2 = 10
    • answer = 0.0

Try It: Aritmetic Operators

  • For each of the following pseudocode algorithms below, in a few sentences, explain their function.
foo ← 100
WHILE foo > 0
   IF foo % 2 = 0 THEN
      OUTPUT foo
      foo = foo - 1
   END IF
ENDWHILE
bar ← 1
baz ← 0

WHILE bar < 100
   bar ← bar + baz
   baz ← bar - baz
   OUTPUT bar
ENDWHILE
X ← 1
Y ← 2
WHILE X < 20
    OUTPUT X
    X ← X + Y
    Y ← Y + 1
ENDWHILE
Y ← 3
FOR X ← 1 TO 5
   Y ← Y + X
ENDFOR

OUTPUT Y
num ← 78
answer ← ''

WHILE num > 0
   r ← num MOD 2
   num ← num / 2
   answer ← STR(r) + answer 
ENDWHILE

OUTPUT answer

Try It: Arithmetic Operations

  • Q1: Work out the answers to the following calculations:

Arithmetic_Operations_Calculations.png

Badge It: Coding Challenge 1

Silver - Code Challenge

  1. Using the Trinket below, create a working program that calculates the values of w, x, y and z. (4 Marks)
  2. Add comments to each line of code explaining what it does. (2 Marks)

Upload to Fundamentals of 3.2 Programming - 3.2.3 Arithmetic, Relational and Boolean Operations in a Programming Language: Silver on BourneToLearn

2 Relational Operations

Learn It: What are Relational Operations?

Relational Operations - A comparison between two values to check whether they are; equal to,
less than or greater than the other value. Relational operations are found in IF statements
and as part of loops.
  • Relational operators can be used to compare numbers as well as strings.
  • Relational operators are also known as comparison operators as they compare expressions on the left-hand side to expressions on the right-hand side and produce a Boolean value of either True or False.
  • Relation and Logic are the fundamental bricks of a program that defines its functionality.
  • With these fundamentals, you decide what should be the flow of execution and what conditions should be kept to make sure the flow stays that way.
  • In every programming language including python, to manage the flow of any program, conditions are required, and to define those conditions, relational and logical operators are required.
  • Relational operators are symbols that perform operations on data and return a result as True or False depending on the comparison conditions.
  • Let's take a look at the different types of relational operators in python. The following table gives you a list of all the relational operators:

Relational_Operators_Table.png

  • One of the main differences is with the first symbol and this is one that people often get wrong.
  • In programming, a common mistake is to get the assignment operator = and the comparison operator == mixed up.
  • You will know when you have used them incorrectly because your code won’t behave as intended.
  • Have a look at the examples below to see the impact of using the wrong operator in an IF statement:

Relational_Assign_Compare.png

  • Examples of using relational operators, input/output, conditional selection, nested IF statements:

Relational_Code_Example1.png Relational_Code_Example2.png

Try It: Relational Operators

Pseudocode for Password Challenge

  • The following Pseudocode asks the user exactly three times for the user to enter the correct password.
  • Q1: Using the Pseudocode and the Trinket window below, create a working program in Python.

Relational_Password_Task1.png Password Challenge 1

  • Q2: How many times will the loop be performed if the user enters the correct password on the first attempt?
  • Q3: Work out if the following statements are true or false:

Relational_Operators_Try_It.png

Badge It: Coding Challenge 2

Gold - Password Code Challenge

  1. Using the Trinket below, rewrite the algorithm given in Password Challenge 1 using a WHILE loop to allow the user up to three attempts to correctly guess the password. (4 Marks)

Relational_Password_Task2.png

Upload to Fundamentals of 3.2 Programming - 3.2.3 Arithmetic, Relational and Boolean Operations in a Programming Language: Gold on BourneToLearn

3 Boolean Operations

Learn It: What are Boolean Operations?

Boolean Operations - A logic operation can only have one of two possible
outcomes - True or False. A logic operator connects together other logic
operators to produce more complexed logic expressions. NOT, AND, OR are the
most commonly used logic operators.
  • In computer science, a Boolean data type is any data type that has either a True or False value, Yes or No, or On or Off (1 or 0) value.
  • In programming, a Boolean can be used with conditional statements (i.e. IF statements)
  • Boolean operators can be either True or False. It makes no sense to perform mathematical operations on them or to compare them to see which is greater.
  • With Boolean(logical)operators we can create Boolean expressions by combining together multiple logic gates.
  • Let's take a look at the different types of boolean operators in python. The following table gives you a list of the three main logic operators we will be using:

Boolean_Operators_Table.png

  • AND will only return True if both operands (the two Boolean objects you are comparing) are True. For example:

AND_Statement.png AND_Result.png

  • To understand the behaviour of the AND logical operator, you can make use of a truth table:

AND_Truth_Table.png

  • OR is True, whenever any (one or more) operand is True. For example:

OR_Statement.png OR_Result.png

  • The OR logical operator uses the following truth table:

OR_Truth_Table.png

  • Notice that these also work with more than two operands. For example:

AND_OR_Statement.png AND_OR_Result.png

  • NOT will return the opposite of its operand, so if False is given then True will be returned and vice-versa. For example:

NOT_Statement.png NOT_Result.png

  • The NOT logical operator uses the following truth table:

NOT_Truth_Table.png

  • The following Python code combines all three types of operators.
  • It calculates the cost of a group of adults and children entering a zoo.
  • Q1: How much would it cost for two adults and one child?

Badge It: Coding Challenge 3

Platinum - Zoo Code Challenge

  1. Using the Trinket below, add additional lines of code that would allow a concessionary rate. (4 Marks)
  2. And add comments to each line of code to explain what function it performs? (2 Marks)

Upload to Fundamentals of 3.2 Programming - 3.2.3 Arithmetic, Relational and Boolean Operations in a Programming Language: Platinum on BourneToLearn

Try It: Coding Challenge

  1. Using the Trinket below, write a program which tests someone on the powers of 2 up to 212. (6 Marks)
  2. And add comments to each line of code to explain what function it performs? (2 Marks)

Validate