1 / 15

Arithmetic in Pascal

Arithmetic in Pascal. A Short Glance. We will learn the followings in this chapter Arithmetic operators Order of precedence Assignment statements Arithmetic functions ( in the next lesson ). Arithmetic Operators. Addition + Subtraction - (unary minus) Multiplication *

vinnie
Download Presentation

Arithmetic in Pascal

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arithmetic in Pascal

  2. A Short Glance • We will learn the followings in this chapter • Arithmetic operators • Order of precedence • Assignment statements • Arithmetic functions ( in the next lesson )

  3. Arithmetic Operators • Addition + • Subtraction - (unary minus) • Multiplication * • Real Division / • Integer Division div • Modulo (modulus) mod

  4. Addition • 1 + 2 • NumA + NumB

  5. Subtraction and Unary Minus • 45 – 20 • NumB – NumA • -20 + -NumC Unary minus

  6. Multiplication • 10 * 5 • NumA * NumB • 2Y 2 * Y

  7. 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

  8. Modulo (Modulus) • The result is the remainder of a division operation • 9 mod 5  4 • 13 mod 3  1

  9. Order of Precedence • Highest • - (unary minus) • High • *, /, div, mod • Low • +, -

  10. 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

  11. Order of Precedence (examples) • 6 + 3 * 4 • 6 + 12 • 3 * 5mod 2 • 15 mod 2 • ( 10 + 20 ) / 5 • 30 / 5

  12. Assignment Statements • Assignment means assigning a value to a variable • Syntax: • <variable> := <expression> • Please don’t use = , use :=

  13. Accumulation • N := N + 1; • The value of N will be increased by 1 after this operation • Never write this in your Mathematics works

  14. 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

  15. An Example program arithmetic; const Half = 0.5; var Int1, Int2, Int3 : Integer; Real1, Real2 : Real; begin Int1 := 10; Int2 := Int1 + 15; Int3 := Int2 mod Int1; writeln ( Int2 ); writeln ( Int3 ); Real1 := Int1 + Int2 * 2; Real2 := Int1 * Half; writeln ( Real1:2:2 ); writeln ( Real2:2:2 ); end.

More Related