topic 2 student copy

Upload: saltihie-zeni

Post on 06-Apr-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Topic 2 Student Copy

    1/46

    Prepared by: Saltihie Bin Zeni

    Jabatan Kej. Elektrik, PMU3 2 1 Start

  • 8/3/2019 Topic 2 Student Copy

    2/46

    OVERVIEW

    2.1

    Understand constant andvariables

    2.2

    2.3

    Understand operator andexpression

  • 8/3/2019 Topic 2 Student Copy

    3/46

    2.1 Understand constant and variable

    Constant

    Values that do not change during program execution. Example : conts double PI = 3.14159 // conts is a

    reserved word

    Variables Variables are like small blackboards

    We can write a number on them

    We can change the number We can erase the number

    Example: int answer;

  • 8/3/2019 Topic 2 Student Copy

    4/46

    Identifiers Identifiers

    Variables names are called identifiers Choosing variable names

    Use meaningful names that represent data to

    be stored

    First character must be a letter

    the underscore character

    Remaining characters must be

    letters

    numbers

    underscore character

  • 8/3/2019 Topic 2 Student Copy

    5/46

    Example of identifiers

    Identifier Valid Reason if Invalid

    TotalSales Yes

    _

    Total.sales No Cannot contains .

    4Qthrsales No Cannot begin with

    numberTotal_sales$ No Cannot contain $

  • 8/3/2019 Topic 2 Student Copy

    6/46

    Keyword Keywords (also called reserved words)

    Are used by the C++ language

    Must be used as they are defined in the

    Cannot be used as identifiers

    Examples of keyword:

    asm class do auto conts break char huge while else case float if far

  • 8/3/2019 Topic 2 Student Copy

    7/46

    Variables declaration (1) Before use, variables must be declared

    Tells the compiler the type of data to store

    Examples: int number_of_bars;

    _ , _

    int is an abbreviation for integer.

    could store 3, 102, 3211, -456, etc.

    number_of_bars is of type integer

    double represents numbers with a fractionalcomponent

    could store 1.34, 4.0, -345.6, etc.

    one_weight and total_weight are both of type double

  • 8/3/2019 Topic 2 Student Copy

    8/46

    Immediately prior to use

    At the beginningTwo locations for variable declarations

    int main(){

    Variables declaration (2)

    {int sum;sum = score1 +

    score2;

    return 0;

    }

    int sum;sum = score1 + score 2;

    return 0;}

  • 8/3/2019 Topic 2 Student Copy

    9/46

    Declaration syntax:

    Data_type Variable_1 , Variable_2, . . . ;

    Variables declaration (3)

    double average, m_score, total_score;

    double moon_distance;

    int age, num_students;int cars_waiting;

  • 8/3/2019 Topic 2 Student Copy

    10/46

    Assignment statement An assignment statement changes the value of a variable

    total_weight = one_weight + number_of_bars; total_weight is set to the sum one_weight + number_of_bars

    Assignment statements end with a semi-colon

    The single variable to be changed is always on the leftof the assignment operator =

    On the right of the assignment operator can be

    Constants -- age = 21; Variables -- my_cost = your_cost;

    Expressions -- circumference = diameter * 3.14159;

  • 8/3/2019 Topic 2 Student Copy

    11/46

    The = operator in C++ is not an equal sign

    The following statement cannot be true in algebra

    Assignment statement and Algebra

    =

    In C++ it means the new value of number_of_bars

    is the previous value of number_of_bars plus 3

    _ _ _ _

  • 8/3/2019 Topic 2 Student Copy

    12/46

    Exercise 1 Give good variable names for identifiers to store.

    the speed of an automobile? an hourly pay rate?

    the highest score on an exam?

    Identify the following identifiers is valid or not. Net_income

    4years_sale

    Hour_p#y Year_1income

    Y2kilo

  • 8/3/2019 Topic 2 Student Copy

    13/46

    2.2 Understand data types 2 and 2.0 are not the same number

    A whole number such as 2 is of type intA real number such as 2.0 is of type double

    Numbers of type int are stored as exact values

    Numbers of type double may be stored as

    approximate values due to limitations on number of

    significant digits that can be represented

  • 8/3/2019 Topic 2 Student Copy

    14/46

    Data type (1)Integer (int)

    Integer data types hold whole numbers

    Type int does not contain decimal points

    Examples: 34 45 1 89

    a e e ow s ows an n eger a a ypes, s ze an range

  • 8/3/2019 Topic 2 Student Copy

    15/46

    Data type (2)Float/double

    The floating-point data types are:

    They can hold real numbers such as:

    12.45 -3.8Stored in a form similar to scientific notation

    All floating-point numbers are signed

  • 8/3/2019 Topic 2 Student Copy

    16/46

    Data type (3) Type float/double can be written in two ways

    Simple form must include a decimal point Examples: 34.1 23.0034 1.0 89.9

    Data type (3)

    Examples: 3.41e1 means 34.13.67e17 means 367000000000000000.0

    5.89e-6 means 0.00000589

    Number left of e does not require a decimal point

    Exponent cannot contain a decimal point

  • 8/3/2019 Topic 2 Student Copy

    17/46

    Example:

    Data type (4)

  • 8/3/2019 Topic 2 Student Copy

    18/46

    Character (char)

    Used to hold characters or very small integervalues

    Data type (5)

    Usua y 1 yte o memory Numeric value of character from the character

    set is stored in memory:

  • 8/3/2019 Topic 2 Student Copy

    19/46

    Example

    Data type (6)

  • 8/3/2019 Topic 2 Student Copy

    20/46

    2.3 : Know operators & expressionPRIMARY EXPRESSION

    -

    constantIdentifier expression

  • 8/3/2019 Topic 2 Student Copy

    21/46

    ASSIGNMENT EXPRESSION

    a =

    variable Assignmentoperatorsexpression

  • 8/3/2019 Topic 2 Student Copy

    22/46

    BINARY EXPRESSION

    +

    Firstoperand

    operator Secondoperand

  • 8/3/2019 Topic 2 Student Copy

    23/46

    POSTFIX OPERATOR

    operand

    a++

    Postfix operator

    (increment by 1)

  • 8/3/2019 Topic 2 Student Copy

    24/46

    PREFIX EXPRESSION

    operand

    ++a

    unary operator

  • 8/3/2019 Topic 2 Student Copy

    25/46

    Operator Can be classified according to:

    The type of their operands and their output. Arithmetic

    Logical

    Boolean

    The number of their operand.

    Unary(one operand)

    Binary(two operands)

  • 8/3/2019 Topic 2 Student Copy

    26/46

    1. Arithmetic Operators (2)

  • 8/3/2019 Topic 2 Student Copy

    27/46

    Arithmetic Operators (1)

  • 8/3/2019 Topic 2 Student Copy

    28/46

    Arithmetic Operators (3)

  • 8/3/2019 Topic 2 Student Copy

    29/46

    Arithmetic Operators (4)

  • 8/3/2019 Topic 2 Student Copy

    30/46

    Arithmetic Operators (5)

  • 8/3/2019 Topic 2 Student Copy

    31/46

    Arithmetic Operators (6)

  • 8/3/2019 Topic 2 Student Copy

    32/46

    Example of Arithmetic operator

    Arithmetic Operators (7)

  • 8/3/2019 Topic 2 Student Copy

    33/46

    Arithmetic Operators (8)

  • 8/3/2019 Topic 2 Student Copy

    34/46

    Arithmetic Operators (9)

  • 8/3/2019 Topic 2 Student Copy

    35/46

    Arithmetic Operators (10)

  • 8/3/2019 Topic 2 Student Copy

    36/46

    Exercise 2

  • 8/3/2019 Topic 2 Student Copy

    37/46

    2. Relational Operator (1)

  • 8/3/2019 Topic 2 Student Copy

    38/46

    Relational Operator (2)

  • 8/3/2019 Topic 2 Student Copy

    39/46

    Relational Operator (3)

  • 8/3/2019 Topic 2 Student Copy

    40/46

    3. Logical Operator (1)

  • 8/3/2019 Topic 2 Student Copy

    41/46

    Logical Operator (2)

  • 8/3/2019 Topic 2 Student Copy

    42/46

    Logical Operator (3)

  • 8/3/2019 Topic 2 Student Copy

    43/46

    Formed by combining operators, variables and

    constant. Examples:

    Expression

    ross_pay e uc ons

    (basic_pay + hours * rate) (socso + premium + loan)

    (b * b 4 * a * c) > 0

    (sex == male || sex == female) && age >=21

  • 8/3/2019 Topic 2 Student Copy

    44/46

    Mathematical formula and C expression

    Mathematical operator C expression

    + +- -

    *

    /

    OR ||

    AND &&NOT !

  • 8/3/2019 Topic 2 Student Copy

    45/46

    Example: Given the following formula

    LCF

    2832.6

    1=

    Convert to C expressionF = 1 / (6.2832 * sqrt (L * C))

  • 8/3/2019 Topic 2 Student Copy

    46/46

    Exercise 3 Convert the following equation to C expression:

    i. x = m2(m-q) + (p-n)

    ii.

    iii. -

    5

    2

    = xy

    )10

    5.5( = xyw

    Given integer variables, a = 5, b=7 and c=8, find thefollowing:

    a / b + c

    a + b x (c / a)

    b % a + c

    a b + c x (b / a x c)