Chapter 4 Part 2 utp

download Chapter 4 Part 2 utp

of 22

Transcript of Chapter 4 Part 2 utp

  • 8/10/2019 Chapter 4 Part 2 utp

    1/22

    Chapter 4

    Introduction to Pascal

    Programming Language

    Arithmetic Expressions

    Logical/Boolean ExpressionAssignment Statement

  • 8/10/2019 Chapter 4 Part 2 utp

    2/22

    Page 2 of 22

    Learning Outcomes

    Upon completion of this lecture learners willbe able to:

    convert mathematics expressions into Pascal

    arithmetic expressions,

    evaluate Pascal arithmetic expressions based on

    operator precedence.

  • 8/10/2019 Chapter 4 Part 2 utp

    3/22

    Page 3 of 22

    Pascal Arithmetic

    Unary

    Binary

    Syntax

    Example 5 + 2

    Syntax

    Example -5

  • 8/10/2019 Chapter 4 Part 2 utp

    4/22

  • 8/10/2019 Chapter 4 Part 2 utp

    5/22

    Page 5 of 22

    Operand Data TypesOperator Type of

    First

    Operand

    Type of

    Second

    Operand

    Type of Result

    + Integer /real

    Integer / real Integerboth integer

    Real either real

    - Integer /real

    Integer / real Integer

    both integerReal either real

    * Integer /real

    Integer / real Integerboth integer

    Real either real

    / Integer /real Integer / real Real

    Div Integer Integer Integer

    Mod Integer Integer integer

  • 8/10/2019 Chapter 4 Part 2 utp

    6/22

    Page 6 of 22

    Examples

    Operationresult

    6 + 2 8

    6.2 + 2 8.2

    6 - 2 4

    6.0 - 2 4.0

    6 * 2 12

    6.0 * 2 12.0

    6 / 2 3.0

    7 div 2 3

    7 mod 2 1

  • 8/10/2019 Chapter 4 Part 2 utp

    7/22

    Page 7 of 22

    Relational Operator

    Operator Description example result

    = Equals to 5 = 2 false

    > Greater than 5 > 2 true

    < Less than 5 < 2 false

    >= Greater than or

    equals to

    5.0 >= 2.0 true

  • 8/10/2019 Chapter 4 Part 2 utp

    8/22

    Page 8 of 22

    Boolean Operator

    In some cases, we need to combine more than onerelational operation, for example:

    In Math we write 0 marks 100

    In Pascal we write

    (marks >= 0) AND (marks

  • 8/10/2019 Chapter 4 Part 2 utp

    9/22

    Page 9 of 22

    Truth Table

    Operand1 Operand2 AND OR

    True True True True

    True False False True

    False True False True

    False False False False

  • 8/10/2019 Chapter 4 Part 2 utp

    10/22

    Page 10 of 22

    Operator Precedence

    Priority Operator1 NOT

    2 Unary

    3 *, /, div, mod, AND

    4 +, -, OR

    5 =, , =

    6 Left to right

    If equal precedence, apply left to right rule

  • 8/10/2019 Chapter 4 Part 2 utp

    11/22

    Page 11 of 22

    Arithmetic Evaluation

    7 + 3 * 2-----

    6

    -------13

  • 8/10/2019 Chapter 4 Part 2 utp

    12/22

    Page 12 of 22

    .. Arithmetic Evaluation

    7.0 + 3 DIV 2-------

    1

    ----------8.0

  • 8/10/2019 Chapter 4 Part 2 utp

    13/22

    Page 13 of 22

    .. Arithmetic Evaluation

    9 - 4 / 2 MOD 2-----

    2.0

    ----------ERROR*

    *MOD accepts integer operands only

  • 8/10/2019 Chapter 4 Part 2 utp

    14/22

    Page 14 of 22

    .. Arithmetic Evaluation

    9 - 4 DIV 2 MOD 2-------

    2

    ----------0

    ------------

    9

  • 8/10/2019 Chapter 4 Part 2 utp

    15/22

    Page 15 of 22

    Arithmetic Evaluation

    5 > 6 - 2-----

    4

    -------TRUE

  • 8/10/2019 Chapter 4 Part 2 utp

    16/22

    Page 16 of 22

    Arithmetic Evaluation

    5 > 6 AND 2

  • 8/10/2019 Chapter 4 Part 2 utp

    17/22

    Page 17 of 22

    Arithmetic Evaluation

    (5 > 6) AND (2

  • 8/10/2019 Chapter 4 Part 2 utp

    18/22

    Page 18 of 22

    Arithmetic Functions

    Pascal provides predefined functions that performcommon operations

    Have to include the unit/library before the var

    section

    uses math;

    Syntax ()

    Example sqrt(49)

    power (5, 2)

  • 8/10/2019 Chapter 4 Part 2 utp

    19/22

    Page 19 of 22

    .. Arithmetic FunctionsFunction Description Type of

    Argument

    Type of

    result

    Example

    Abs(x) Absolute value of x

    |x|

    Integer/

    real

    Same Abs(-9) = 9

    Abs(-3.5) = 3.5

    Sqrt(x) Positive square

    root of x

    (nonve)

    Integer/

    real

    Real Sqrt(9) = 3.0

    Sqrt(-3.5) = Error!!

    Round(x) Round to nearest

    integer

    Real Integer Round(3.4) = 3

    Round (-3.5) = -4

    Trunc(x) Truncated to a wholenumber

    Real Integer Trunc(-3.4) = -3

    Random(x)A random whole

    number between 0 to

    x-1 inclusively

    Integer Integer Random(-7) = Error!!

  • 8/10/2019 Chapter 4 Part 2 utp

    20/22

    Page 20 of 22

    .. Arithmetic FunctionsFunction Type of

    Argument

    Type of

    result

    Example Results

    ceil(x) Integer/

    real

    Integer Ceil(9.2)

    Ceil (-3.2)

    10

    -3

    floor(x) Integer/

    Real

    Integer floor(9.9)

    floor(-3.5)

    9

    -4

    power(x,y) Integer/

    Real

    same Power(9,3)

    Power(9.1,2)

    729

    82.81

    Sqr(x) Integer/

    Real

    same Sqr(9)

    Sqr(9.1)

    81

    82.81

    Refer to: http://www.freepascal.org/docs-

    html/rtl/math/index-5.html

    http://www.freepascal.org/docs-html/rtl/math/index-5.htmlhttp://www.freepascal.org/docs-html/rtl/math/index-5.htmlhttp://www.freepascal.org/docs-html/rtl/math/index-5.htmlhttp://www.freepascal.org/docs-html/rtl/math/index-5.htmlhttp://www.freepascal.org/docs-html/rtl/math/index-5.htmlhttp://www.freepascal.org/docs-html/rtl/math/index-5.htmlhttp://www.freepascal.org/docs-html/rtl/math/index-5.html
  • 8/10/2019 Chapter 4 Part 2 utp

    21/22

    Page 21 of 22

    Assignment Statement

    Use assignment operator := Read as Is assigned a value of

    Eg.Variable1 := 999 * 2 + 1;

    The type of the value on RHS of anassignment statement must compatible with

    the type of the variable on LHS, otherwise

    mismatch error When a new value is assigned to the variable,

    the old value is replaced by the new one

  • 8/10/2019 Chapter 4 Part 2 utp

    22/22

    Page 22 of 22

    Declare 3 variables of type string (StudentName1,StudentName2, StudentName3 ) and 2 variables to

    store 2 test results.

    Your program should be able to calculate the

    average value of the StudentName1,StudentName2, StudentName3

    Ask users to key in their names. Assign that 3

    names into the first 3 variables. Ask from eachusers for 2 test results.

    Calculate and display the average of test results for

    each student..

    Exercise