Operators in Algorithms: Arithmetic, Relational and Logical Operators, Truth Tables, MOD and DIV

Algorithms do calculations and make decisions using operators. In this post you will learn the arithmetic operators (including MOD and DIV), the relational operators used to compare values, and the logical operators AND, OR, NOT with their truth tables.

Build on what you learned in Forms 1–3

These Lower School ICT posts cover the basics of this topic:

In this post:

  • Arithmetic operators, MOD and DIV
  • Relational operators
  • Logical operators AND, OR, NOT
  • Truth tables
  • Using operators in pseudocode and code

Arithmetic operators

OperatorMeaningExampleResult
+Add7 + 310
−Subtract7 − 34
*Multiply7 * 321
/Divide7 / 23.5
DIVWhole-number (integer) division – how many times it goes in17 DIV 53
MODRemainder after whole-number division17 MOD 52

MOD and DIV are very useful. Example: a fete has 135 minutes of music. 135 DIV 60 = 2 hours and 135 MOD 60 = 15 minutes. Another common use: a number is even if number MOD 2 = 0.

Pseudocode

READ total_minutes
hours ← total_minutes DIV 60
minutes ← total_minutes MOD 60
PRINT total_minutes, " minutes = ", hours, " hours and ", minutes, " minutes"

Python

# Convert minutes into hours and minutes using DIV and MOD
total_minutes = int(input("Enter the number of minutes: "))
hours = total_minutes // 60      # DIV
minutes = total_minutes % 60     # MOD
print(total_minutes, "minutes =", hours, "hours and", minutes, "minutes")

C

/* Convert minutes into hours and minutes using DIV and MOD */
#include <stdio.h>

int main(void)
{
    int total_minutes, hours, minutes;

    printf("Enter the number of minutes: ");
    scanf("%d", &total_minutes);
    hours = total_minutes / 60;      /* DIV (integer division) */
    minutes = total_minutes % 60;    /* MOD (remainder) */
    printf("%d minutes = %d hours and %d minutes\n", total_minutes, hours, minutes);
    return 0;
}

Sample run with 135: 135 minutes = 2 hours and 15 minutes. In Python, DIV is // and MOD is %; in C, / between two integers does DIV and % is MOD.

Relational operators

Relational operators compare two values. The answer is always TRUE or FALSE.

CSEC pseudocodeMeaningPythonC
<less than<<
>greater than>>
=equal to====
<=less than or equal to<=<=
>=greater than or equal to>=>=
<>not equal to!=!=

Example: IF age >= 17 THEN PRINT "You may apply for a learner’s permit". You can practise these in Relational Operators – A practical Exercise.

Logical operators and truth tables

  • AND – TRUE only if both conditions are TRUE.
  • OR – TRUE if at least one condition is TRUE.
  • NOT – reverses the value: NOT TRUE = FALSE.

A truth table shows the result for every possible combination of inputs.

ABA AND BA OR B
TRUETRUETRUETRUE
TRUEFALSEFALSETRUE
FALSETRUEFALSETRUE
FALSEFALSEFALSEFALSE
ANOT A
TRUEFALSE
FALSETRUE

Everyday example: A child gets into the Carnival kiddies’ fete free IF they are under 12 AND with an adult. A 10-year-old alone → TRUE AND FALSE = FALSE, so no free entry. A scholarship is given IF average >= 80 OR the student won the science fair – only one needs to be TRUE.

Pseudocode

READ age, with_adult
IF age < 12 AND with_adult = TRUE THEN
    PRINT "Free entry"
ELSE
    PRINT "Pay at the gate"
ENDIF

In Python these are written and, or, not; in C they are &&, || and !.

Optional extension: how computers use AND, OR and NOT (Boolean logic) at the hardware level.

Video: Boolean Logic & Logic Gates: Crash Course Computer Science #3 – https://www.youtube.com/watch?v=gI-qXk7XojA

Try it: Complete the truth table

Type TRUE or FALSE in each box, then check.

ABA AND BA OR BNOT A
TRUETRUE
TRUEFALSE
FALSETRUE
FALSEFALSE

Try it: MOD and DIV practice

Work out each answer.

ExpressionAnswer
17 DIV 5
17 MOD 5
20 MOD 4
9 DIV 2
250 MOD 60

Related

Related: Relational Operators – A practical Exercise

Paper 1 practice (multiple choice)

Choose the best answer for each question. Try them all before you check.

1. What is the value of 23 MOD 4?
(A) 5
(B) 3
(C) 5.75
(D) 4

2. What is the value of 23 DIV 4?
(A) 5
(B) 3
(C) 5.75
(D) 6

3. Which relational operator means “not equal to” in CSEC pseudocode?
(A) =
(B) <>
(C) >=
(D) !

4. If A is TRUE and B is FALSE, what is A AND B?
(A) TRUE
(B) FALSE
(C) Both
(D) Cannot tell

5. If A is FALSE and B is TRUE, what is A OR B?
(A) TRUE
(B) FALSE
(C) NOT B
(D) Cannot tell

Show answers

1. B
2. A
3. B
4. B
5. A

Paper 2 practice (structured question)

A minibus company charges students half price IF they are in uniform AND travelling before 6:00 p.m. A program uses the Boolean variables in_uniform and before_six.

(a) State the result of EACH of the following:

(i) 29 MOD 7 (1 mark)

(ii) 29 DIV 7 (1 mark)

(b) Copy and complete a truth table for in_uniform AND before_six, showing all FOUR combinations. (4 marks)

(c) Explain the difference between the AND and OR logical operators. (2 marks)

(d) Write a pseudocode IF statement that prints “Half price” when a student qualifies, and “Full price” otherwise. (2 marks)

Total: 10 marks

Show answers

(a)

(i) 1 (1 mark)

(ii) 4 (1 mark)

(b)
• TRUE, TRUE → TRUE (1 mark)
• TRUE, FALSE → FALSE (1 mark)
• FALSE, TRUE → FALSE (1 mark)
• FALSE, FALSE → FALSE (1 mark)

(c) AND gives TRUE only when both conditions are TRUE (1 mark); OR gives TRUE when at least one of the conditions is TRUE (1 mark).

(d) IF in_uniform = TRUE AND before_six = TRUE THEN PRINT “Half price” (1 mark); ELSE PRINT “Full price” ENDIF (1 mark).

Next: Testing Algorithms with Trace Tables (Desk Checks and Dry Runs)

Syllabus link: This post is relevant to the CSEC® Information Technology Syllabus (CXC 30/G/SYLL 17), Section 7: Problem-Solving and Program Design, Specific Objective 6 (“represent algorithms in the form of flowchart and pseudocode”).

Attributions to media used in this post:

No third-party images are used in this post. Embedded videos are credited on YouTube to their channels.

Updates

2026/10/16

– Syntax highlighting added to code examples.

© 2026 Vedesh Kungebeharry. All rights reserved.

Truth Tables – An application of abstraction to physical circuits to arrive at their logical nature

Warning: I wrote to following paper to maintain a high level of precision when considered  formally under the disciplines of mathematical logic and computer science. This allows for the proper consideration of semantics in future discussions. 

This writing may lead you to believe that you are taken along a platitude of truism devoid of axiomatic purpose that amounts to a march of banality unrivaled in the history of human thought.

Let us begin.

Definition :  A truth table lists  all possible unique inputs to a logic function accompanied by each output value.

A truth table uses the veracity of propositions. Veracity refers to truthfulness, whether the statement is true or false.   A proposition is a statement.

e.g.      let P be the proposition ‘it is raining’.   

            We can assign 2 values to P.  That is,    

P = truemeaning that ‘the statement P is true’
P = falsemeaning ‘the statement P is false.

NOTE:  We can use the following symbols for truth values:

VALUESYMBOL
TrueTrue,  T, 1
FalseFalse, F, 0

In the above example, the proposition for P can be written as the following table:

P
True
False

Note, the proposition has no meaning when taken by itself. Here we only see the possible circumstances described by the statement as it can be used as an input to a logical scenario or taken s the output of a scenario.

I now introduce…

Truth tables in computer science

In this context, Truth tables  can be used to represent the voltages that are applied as input to a circuit and the resulting output.

Though it may seem trivial, we consider a simple circuit with one input or switch , a battery, and a one output or lightbulb:

Simple circuit diagram

Approaching this physical construct of discrete possible events using mathematical logic we say:

Let the input be called I , and we use the convention that a value of 1 means that a high voltage is applied to the bulb and a 0 being the falsehood of “a high voltage is applied to the bulb ” or rather we can say a high voltage is not applied to the bulb or simply no voltage is applied to the bulb.

Another school as thought would describe I having a value 1 as the switch is on and I having a value of 0 as the switch is off.

Let the output of the circuit be O using the convention that a value of 1 means that the bulb is lit and a value of 0 means the falsehood of “the bulb is lit” or rather we can say the bulb is not lit or simply the bulb is unlit.

Yet another school of thought would describe O having a value of 1 as a high voltage was observed at the output and O having a value of 0 as low or no voltage observed at the output

Now we are ready to consider these discrete events devoid of timing to form an enumeration or superposition of sorts that is a truth table.

We arrive at the following truth table which list all possible inputs to the “circuit” and the observed outputs:

IO
11
00

This table tells us,

For an input of 1:

  • If a high voltage is applied to the bulb , the bulb is lit
  • If a high voltage is applied to the bulb then the statement “the bulb is lit” is True
  • If a high voltage is applied to the bulb , a high voltage was observed at the output.
  • If the switch is on, the bulb is lit

For an input of 0

  • If a high voltage is not applied to the bulb, the bulb is unlit
  • If no voltage is applied to the bulb, then the statement “the bulb is lit” is False
  • If no voltage is applied to the bulb , low or voltage was observed at the output.
  • If the switch is off, the bulb is unlit.

Our truth table allows for us to perform an abstraction on our circuit so that we no longer consider the events that physically occur in the circuit, rather the ideas on how the circuit’s output behaves given discrete inputs.

We no longer care about how the circuit is physically implemented; rather we only consider the logical implications of using the particular type of circuit as a building block in other circuits.

Thank you for reaching the end of this paper.

Attributions to media used in this post:

يحيى بن علي, CC BY-SA 4.0, via Wikimedia Commons

© 2021  Vedesh Kungebeharry. All rights reserved.