Lesson 3 of 10
Programming: Lesson 3

Arithmetic and Boolean Operators

More than just plus and minus. This lesson covers the full set of arithmetic operators including MOD and DIV, the boolean operators used to build conditions, and the precedence rules that determine which operations happen first.

50 minutes Python + C# toggle
Language: Saved automatically

The division operator is the most dangerous operator in programming. 7 / 2 gives 3.5 in Python but gave 3 in Python 2. In C# it gives 3 if both numbers are integers, and 3.5 if either is a float. MOD - the remainder after division - is used in dozens of everyday programming tasks: checking if a number is even, wrapping around the end of a list, implementing a cipher, generating a grid position from a flat index. Get the division operators wrong and your program silently computes the wrong answer.

Think about it: Without a computer, work out: what is the remainder when 17 is divided by 5? What about 100 divided by 7? What about 16 divided by 4? Can you spot what MOD is actually telling you in each case?
Terms you need to know
MOD / modulus
The remainder after integer division. Python: %. C#: %. 17 % 5 = 2 (because 17 = 3*5 + 2).
DIV / integer division
Division that discards the remainder, giving a whole number. Python: //. C#: / (when both operands are int).
Exponentiation
Raising to a power. Python: **. C#: Math.Pow(). 2 ** 8 = 256.
Boolean operator
AND, OR, NOT - combine or negate boolean values. Python uses words; C# uses symbols (&&, ||, !).
Comparison operator
== != > < >= <= - compares two values and returns True or False.
Operator precedence
The order in which operators are evaluated. BIDMAS applies. Use brackets to make order explicit.
The full set
OperatorPythonC#ExampleResult
Addition++7 + 310
Subtraction--7 - 34
Multiplication**7 * 321
True division// (floats)7 / 23.5
Integer division (DIV)/// (ints)7 // 23
Modulus (MOD)%%7 % 21
Exponentiation**Math.Pow()2 ** 8256
The critical difference: C# integer division

In C#, 7 / 2 gives 3 (not 3.5) because both operands are integers. To get 3.5, at least one must be a float: 7.0 / 2 or (double)7 / 2. In Python 3, / always gives a float and // always gives an integer - there is no ambiguity. This is a very common source of bugs in C# programs.

# Python arithmetic examples
print(17 % 5)     # 2  (MOD - remainder)
print(17 // 5)    # 3  (DIV - integer quotient)
print(17 / 5)     # 3.4 (true division)
print(2 ** 10)    # 1024 (exponentiation)

# Practical uses of MOD
n = 14
if n % 2 == 0:
    print("even")   # MOD 2: even test

position = n % 7  # 0  (wrap into range 0-6 - useful for circular lists)
// C# arithmetic examples
Console.WriteLine(17 % 5);        // 2  (MOD)
Console.WriteLine(17 / 5);        // 3  (integer DIV - both are ints!)
Console.WriteLine(17.0 / 5);      // 3.4 (true division - one is double)
Console.WriteLine(Math.Pow(2,10)); // 1024.0 (returns double)

int n = 14;
if (n % 2 == 0)
    Console.WriteLine("even");   // MOD 2: even test
Exam angle - MOD and DIV

MOD appears in exam questions constantly. Key uses to recognise: (1) Even/odd test: n MOD 2 = 0 means even. (2) Digit extraction: 173 MOD 10 = 3 (last digit), 173 DIV 10 = 17 (remove last digit). (3) Circular wrap: index MOD length keeps a value in range. (4) Time conversion: 150 DIV 60 = 2 hours, 150 MOD 60 = 30 minutes.

Building conditions

Comparison operators return True or False. Boolean operators combine multiple conditions. Together they form the conditions used in IF statements and WHILE loops.

OperatorPythonC#ExampleResult
Equal to====5 == 5True
Not equal to!=!=5 != 3True
Greater than>>7 > 3True
Less than<<3 < 7True
Greater or equal>=>=5 >= 5True
Less or equal<=<=4 <= 5True
ANDand&&True and FalseFalse
ORor||True or FalseTrue
NOTnot!not TrueFalse
# Python boolean examples
age = 17
has_id = True

# AND: both must be true
if age >= 18 and has_id:
    print("Entry granted")

# OR: at least one must be true
if age < 0 or age > 120:
    print("Invalid age")

# NOT: flip the boolean
if not has_id:
    print("No ID - entry refused")
// C# boolean examples
int age = 17;
bool hasId = true;

// AND: both must be true
if (age >= 18 && hasId)
    Console.WriteLine("Entry granted");

// OR: at least one must be true
if (age < 0 || age > 120)
    Console.WriteLine("Invalid age");

// NOT: flip the boolean
if (!hasId)
    Console.WriteLine("No ID - entry refused");

Operator precedence (BIDMAS + logic):

# Order: brackets first, then ** (power), then */%, then +-, then comparisons, then not, then and, then or
result = 2 + 3 * 4 ** 2    # = 2 + 3 * 16 = 2 + 48 = 50 (NOT 400)
check = 4 > 2 and 10 % 3 == 1   # = True and 1 == 1 = True and True = True

# Always use brackets when mixing arithmetic and logic
safe = (score >= 50) and (grade != "U")
Expression Evaluator
Step-by-Step Expression Evaluator
Enter an arithmetic expression to see it evaluated step by step with MOD, DIV and precedence explained.
Try these examples:
Click Evaluate to see the step-by-step breakdown...

Three Quick Challenges

Predict the output

What is the result of the modulus operation?

print(17 % 5)
Console.WriteLine(17 % 5);
Fill in the blank

Use the floor division operator to get the integer part of 10 divided by 3 (result should be 3, not 3.333...):

result = 10 3
int result = 10 3; // int divided by int gives int automatically
Spot the bug

This condition raises a SyntaxError. What is wrong?

if x = 5:
    print("five")

Test yourself

1. What is the value of 23 MOD 7?

23 = 3 * 7 + 2. So 23 DIV 7 = 3 and 23 MOD 7 = 2. You can verify: 3 * 7 = 21, and 23 - 21 = 2 (the remainder).

2. In C#, what does 9 / 2 evaluate to?

In C#, when both operands are integers, / performs integer division (like // in Python). 9 / 2 = 4 (not 4.5). To get 4.5 you need: 9.0 / 2 or (double)9 / 2. This is a major difference from Python 3 where / always gives a float.

3. What does 2 + 3 * 4 evaluate to in Python?

Multiplication has higher precedence than addition (BIDMAS). So 3 * 4 = 12 is evaluated first, then 2 + 12 = 14. To get 20 you would need brackets: (2 + 3) * 4.

4. Evaluate: True AND False OR True

AND has higher precedence than OR. So this is evaluated as (True AND False) OR True = False OR True = True. If it were True AND (False OR True) = True AND True = True - the same result here, but the precedence rule matters for other combinations.

5. A program checks: if score >= 50 and score <= 100. Which score fails this condition?

The condition requires BOTH: score >= 50 AND score <= 100. A score of 101 fails because 101 <= 100 is False, making the AND False. Scores of 50, 75 and 100 all satisfy both conditions so the AND evaluates to True.
Challenge question

A program needs to convert a number of seconds (e.g. 3750) into hours, minutes and seconds (e.g. 1 hour, 2 minutes, 30 seconds). Write the three calculations using only DIV and MOD. No loops, no libraries - just integer arithmetic. Then verify your answer manually.

Solution:
hours = 3750 // 3600 - gives 1 (3750 divided by seconds-per-hour)
remaining = 3750 % 3600 - gives 150 (leftover seconds after removing whole hours)
minutes = remaining // 60 - gives 2 (150 divided by seconds-per-minute)
seconds = remaining % 60 - gives 30

Verification: 1*3600 + 2*60 + 30 = 3600 + 120 + 30 = 3750. Correct.

Pattern: DIV extracts the whole units; MOD strips them off to get the remainder. This pattern (DIV then MOD) is used in every unit-conversion problem: hours/minutes/seconds, pounds/shillings/pence, degrees/minutes/seconds of arc.
Printable Worksheets

Practice what you have learned

Three levelled worksheets. Download, print and complete offline.

Recall
Operator Reference
Name operators, compute MOD and DIV by hand, and complete truth tables for AND, OR and NOT.
Download
Apply
Expressions and Conditions
Evaluate complex expressions step by step, identify precedence errors and write boolean conditions for real scenarios.
Download
Exam Style
Exam-Style Questions
Trace arithmetic programs, correct operator precedence bugs, write DIV/MOD solutions for time conversion and even/odd tests.
Download
Lesson 3 - Programming
Arithmetic and Boolean Operators
Starter activity
Write on the board: "What is the remainder when 17 is divided by 5?" Let students work it out by hand. Then ask for real-world uses of remainders: checking if a year is a leap year, telling if a number is odd, converting seconds to minutes. This motivates MOD before any code is shown.
Lesson objectives
1
Use all arithmetic operators including %, //, ** and explain the difference between / and // in Python.
2
Explain why C# integer division differs from Python's / operator.
3
Apply MOD to real problems: even/odd, digit extraction, time conversion.
4
Use AND, OR and NOT (Python) and &&, || and ! (C#) to build compound conditions.
5
Apply BIDMAS precedence rules to predict the value of complex expressions.
Key vocabulary
MOD (modulus)
Remainder after integer division. Python: %. C#: %. Uses: even/odd, wrap-around, digit extraction.
DIV (integer division)
Quotient with no remainder. Python: //. C#: / with int operands.
Short-circuit evaluation
With AND, if the left side is False, the right side is not evaluated. With OR, if the left side is True, the right side is skipped.
Discussion questions
Why does 7 / 2 give 3 in C# but 3.5 in Python? Which behaviour do you think is safer for beginners?
Name three real-world scenarios where MOD is the right operator to use. Why not just use division and round?
When should you add brackets to an expression even when they are not technically required?
Exit tickets
What is 256 MOD 10? What is 256 DIV 10? Explain what each tells you. [3 marks]
Write a boolean expression that is True only when x is between 1 and 100 inclusive. [2 marks]
Evaluate: 3 + 4 ** 2 // 2 without a computer, showing each step. [3 marks]
Homework suggestion
Write a program that asks for a number of seconds and outputs the equivalent time in hours, minutes and seconds (e.g. 3750 seconds = 1 hour, 2 minutes, 30 seconds). Use only DIV and MOD - no built-in time libraries. Test with at least three different inputs.