Precedence of Operations. More Loops
Eric Logan Huggins · 1983
In Chapter 2 we listed some of the arithmetical operators which are used in Pascal. They can apply to both constants and variables and some of them (+, −, *) are applicable to constants and variables of type real or of type integer while others are restricted to type real (/) or to type integer (DIV, MOD). Pascal, in common with most other high level languages, awards precedence to multiplication and division operations over those of addition and subtraction. Those with higher precedence are all performed before those with lower precedence. These rules (and one or two others which we shall meet later) are very simple but, as they are different from those of many pocket calculators, they can be a bit confusing at first. For example % MathType!MTEF!2!1!+- % feaaguart1ev2aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLn % hiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqtubsr % 4rNCHbGeaGqiVu0Je9sqqrpepC0xbbL8F4rqqrFfpeea0xe9Lq-Jc9 % vqaqpepm0xbba9pwe9Q8fs0-yqaqpepae9pg0FirpepeKkFr0xfr-x % fr-xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaGaaGOmaiabgU % caRiaaiodacaGGQaGaaGinaaaa!39B9! $$ 2 + 3*4 $$ gives 14 in Pascal since the multiplication takes precedence and will be performed before the addition. On many pocket calculators the result would have been 20. Similarly % MathType!MTEF!2!1!+- % feaaguart1ev2aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLn % hiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqtubsr % 4rNCHbGeaGqiVu0Je9sqqrpepC0xbbL8F4rqqrFfpeea0xe9Lq-Jc9 % vqaqpepm0xbba9pwe9Q8fs0-yqaqpepae9pg0FirpepeKkFr0xfr-x % fr-xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaGaaGymaiaaik % dacqGHsislcaaI2aGaamiraiaadMeacaWGwbGaaG4maaaa!3C45! $$ 12 - 6DIV3 $$ will give 10 in Pascal, not 2.