Quick Quiz #1 - DIV and MOD - Solution

Programming language questions

Quick Quiz #1 - DIV and MOD - Solution

Postby cfbsoftware » Wed Nov 26, 2014 11:39 am

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
cfbsoftware
 
Posts: 32
Joined: Wed Sep 18, 2013 10:06 pm

Return to Component Pascal

Who is online

Users browsing this forum: No registered users and 1 guest

cron