Arithmetic — Pseudocode

The operators (calculation signs) + - * / add, subtract, multiply and divide. / can give a decimal result, written with a dot (7 / 2 is 3.5). DIV divides and…

Rina, Dodi and Sari eat at Mrs. Sri's Stall. The bill is $50.00, and they want to share it equally. In this lesson, money is counted in cents, so every amount is a whole number: the bill is 5000 cents. The calculator on a phone shows 1666.666... Nobody can pay 0.666 of a cent. They really want to know two things: how many whole cents each person pays, and how many cents are left over.

A computer can answer both questions, as long as you use the right operators. Operators are the signs for calculating, such as + and -.

The four basic operations

The four basic operations are adding, subtracting, multiplying and dividing. The result is below the code:

PRINT 12 + 3
PRINT 12 - 3
PRINT 12 * 3
PRINT 7 / 2
15
9
36
3.5

DIV and MOD: whole division and what's left

Here is the bill, divided in three ways:

PRINT 5000 / 3
PRINT 5000 DIV 3
PRINT 5000 MOD 3
1666.66666666667
1666
2

DIV divides and throws the decimals away: each person pays 1666 cents, which is $16.66. MOD gives the remainder (what is left after dividing), which is 2: 1666 times 3 is 4998, still 2 cents less than 5000. Either Rina adds those 2 cents, or Mrs. Sri does not ask for them.

Before you read on, guess: what are 370 DIV 100 and 370 MOD 100? The answer is 3 and 70. This means that change of 370 cents is three coins of 100 cents (three dollar coins), with 70 left over. That is the pattern you'll use in the exercise.

MOD is also an easy way to check whether a number is even or odd: 8 MOD 2 is 0 (even), 7 MOD 2 is 1 (odd). Later, in Python, DIV is written // and MOD is written %. JavaScript has no DIV. For a DIV b, write Math.floor(a / b) (divide, then round down). JavaScript's % gives the same result as MOD as long as both numbers are positive. With negative numbers, the results differ. DIV rounds down, so -7 DIV 2 is -4, and -7 MOD 2 is 1, as in Python. But in JavaScript, -7 % 2 is -1. So in JavaScript, check whether a number x is odd with x % 2 !== 0 (the remainder is not 0), not with x % 2 === 1 (the remainder is 1).

What is calculated first

These two lines use the same numbers, but give different results:

PRINT 2 + 3 * 4
PRINT (2 + 3) * 4
14
20

As in school maths, *, /, DIV and MOD are calculated first, then + and -. Brackets are calculated before everything else. So the first line multiplies 3 by 4 first, while the second line adds 2 and 3 first.

At the stall, this default order is sometimes right and sometimes causes a mistake. For example, the price in cents of one plate of rice and two iced teas:

rice ← 1200
tea ← 400
PRINT rice + 2 * tea
PRINT (rice + 2 * tea) * 90 / 100
PRINT rice + 2 * tea * 90 / 100
2000
1800
1920

The third line calculates one plate of rice and two iced teas: 2000, and the default order is already right. The fourth line gives a 10 percent discount (the customer pays 90 percent: times 90, then divided by 100), and it needs the brackets so that the total is calculated first. In the fifth line, without brackets, the discount applies only to the iced teas, so the result is 1920.

Storing a result

A result can be stored in a variable and used again:

price ← 2750
paid ← 3000
change ← paid - price
PRINT "Change:", change
PRINT "Coins of 100:", change DIV 100
PRINT "Left over:", change MOD 100
Change: 250
Coins of 100: 2
Left over: 50

change is calculated once, then used three times. If the price changes, you only change one line.

⚠️ Why is "Price:" + price refused?

TL;DR: The operators (calculation signs) + - * / add, subtract, multiply and divide. / can give a decimal result, written with a dot (7 / 2 is 3.5). DIV divides and throws the decimals away (5000 DIV 3 is 1666), and MOD gives the remainder, what is left after dividing (5000 MOD 3 is 2). *, /, DIV, MOD are calculated before + and -, and brackets are calculated first of all. A result can be stored in a variable: change ← paid - price. Text can't be added to a number: use a comma in PRINT, or turn the number into text with TEXT(price). Write numbers without a thousands dot.

⚡ The computer's maths signs

Write:

PRINT 12 + 3
PRINT 12 - 3
PRINT 12 * 3
PRINT 7 / 2

Meaning: Adding and subtracting use + and -. Multiply with *, not x. Divide with /, not :. / can give a decimal result, written with a dot: 7 / 2 is 3.5.

⚡ DIV and MOD

Write:

PRINT 5000 / 3
PRINT 5000 DIV 3
PRINT 5000 MOD 3
Guess first: DIV divides and throws the decimals away, MOD gives the remainder (what is left after dividing). What are 370 DIV 100 and 370 MOD 100?

3 and 70. Change of 370 cents is three coins of 100 cents (three dollar coins), with 70 left over. You'll use this pattern in the exercises.

Meaning: For 5000 divided by 3, DIV gives 1666 and MOD gives the remainder, 2.

⚡ What is calculated first

Write:

PRINT 2 + 3 * 4
PRINT (2 + 3) * 4

Meaning: The results are 14, then 20. *, /, DIV and MOD are calculated before + and -, and brackets are calculated first of all. Example: for a discount on the whole bill, add up the bill inside brackets first.

⚡ Text can't be added to a number

Meaning: If price holds a number, PRINT "Price:" + price stops with an error, because + can't add text to a number. Use a comma: PRINT "Price:", price. Write numbers without a thousands dot: 1500, not 1.500.

What you will learn