Quick Quiz #1 - DIV and MOD - Solution

Programming language questions
Post Reply
cfbsoftware
Posts: 55
Joined: Wed Sep 18, 2013 10:06 pm
Contact:

Quick Quiz #1 - DIV and MOD - Solution

Post by cfbsoftware »

1. The main issue is one of operator precedence. The monadic - operator has a lower precedence than the multiplicative operators DIV and MOD. Hence:

Code: Select all

q := -10 DIV 3;
r := -10 MOD 3;
are equivalent to:

Code: Select all

q := -(10 DIV 3);
r := -(10 MOD 3);
so:

Code: Select all

q = -3
r = -1
2. However:

Code: Select all

x := -10;
q := x DIV 3;
r := x MOD 3;
is evaluated as:

Code: Select all

q := (-10) DIV 3;
r := (-10) MOD 3;
so in this case:

Code: Select all

q = -4
r = 2
Post Reply