Download presentation
Presentation is loading. Please wait.
Published byBrandon Williams Modified over 9 years ago
2
Arithmetic in Pascal
3
A Short Glance We will learn the followings in this chapter Arithmetic operators Order of precedence Assignment statements Arithmetic functions ( in the next lesson )
4
Arithmetic Operators Addition+ Subtraction-(unary minus) Multiplication* Real Division/ Integer Divisiondiv Modulo(modulus)mod
5
Addition 1 + 2 NumA + NumB
6
Subtraction and Unary Minus 45 – 20 NumB – NumA -20 + -NumC Unary minus
7
Multiplication 10 * 5 NumA * NumB 2Y 2 * Y
8
Division Real division The result value is of type real 5 / 2 2.5 Integer division The result value is of type integer Round down to the nearest integer 5 div 2 2
9
Modulo (Modulus) The result is the remainder of a division operation 9 mod 5 4 13 mod 3 1
10
Order of Precedence Highest - (unary minus) High *, /, div, mod Low +, -
11
Order of Precedence High-precedence operations are performed first Operators with the same precedence are performed from left to right You could use parentheses to force the expressions within them to be performed first
12
Order of Precedence (examples) 6 + 3 * 4 6 + 12 3 * 5 mod 2 15 mod 2 ( 10 + 20 ) / 5 30 / 5
13
Assignment Statements Assignment means assigning a value to a variable Syntax: := Please don’t use =, use :=
14
Accumulation N := N + 1; The value of N will be increased by 1 after this operation Never write this in your Mathematics works
15
Mixed type assignment Variables must be assigned values of the same type Only one exception: You can assign an integer value to a real variable What will be the type of the variable after the assignment operation? You can’t assign a real value to an integer variable
16
An Example program arithmetic; constHalf = 0.5; var Int1, Int2, Int3 : Integer; Real1, Real2 : Real; begin Int1 := 10; Int2 := Int1 + 15; writeln ( Int2 ); Real1 := Int1 + Int2 * 2; Real2 := Int1 * Half; writeln ( Real2:2:2 ); end. Int3 := Int2 mod Int1; writeln ( Int3 ); writeln ( Real1:2:2 );
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.