chapter4 basic c operators-new

Upload: muhd-rzwan

Post on 14-Apr-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 Chapter4 Basic C Operators-New

    1/21

    Principles of Programming - NI July2005 1

    Chapter 4: Basic C Operators

    In this chapter, you will learn about:Arithmetic operators

    Unary operators

    Binary operatorsAssignment operators

    Equalities and relational operators

    Logical operators

    Conditional operator

  • 7/29/2019 Chapter4 Basic C Operators-New

    2/21

    Principles of Programming - NI July2005 2

    Arithmetic Operators I

    In C, we have the following operators (notethat all these example are using 9 as thevalue of its first operand)

  • 7/29/2019 Chapter4 Basic C Operators-New

    3/21

    Principles of Programming - NI July2005 3

    Arithmetic Operators II

    There are 2 types of arithmetic operatorsin C:

    unary operators

    operators that require only one operand.binary operators.

    operators that require two operands.

  • 7/29/2019 Chapter4 Basic C Operators-New

    4/21

    Principles of Programming - NI July2005 4

    Unary Operator

    C Operation Operator ExamplePositive + a = +3

    Negative - b = -a

    Increment ++ i++

    Decrement -- i--

    The first assigns positive 3 to a

    The second assigns the negative value of a to b.

    i++ is equivalent to i = i + 1

    i-- is equivalent to i = i-1

  • 7/29/2019 Chapter4 Basic C Operators-New

    5/21

    Principles of Programming - NI July2005 5

    PRE- / POST-Increment

    It is also possible to use ++i and --i instead ofi++ and i--

    However, the two forms have a slightly yetimportant difference.

    Consider this example:int a = 9;

    printf(%d\n, a++);

    printf(%d, a);

    The output would be:

    9

    10

  • 7/29/2019 Chapter4 Basic C Operators-New

    6/21

    Principles of Programming - NI July2005 6

    PRE- / POST-Increment cont

    But if we have:int a = 9;

    printf(%d\n, ++a);

    printf(%d, a);

    The output would be:

    10

    10

    a++ would return the current value ofa

    andthen increment the value ofa

    ++a on the other hand increment the value ofa before returning the value

  • 7/29/2019 Chapter4 Basic C Operators-New

    7/21

    Principles of Programming - NI July2005 7

    The following table illustrates the difference between the prefix and postfixmodes of the increment and decrement operator.

    int R = 10, count=10;

    ++ Or --Statement

    EquivalentStatements

    R valueCountvalue

    R = count++; R = count;

    count = count + 1 10 11

    R = ++count; count = count + 1;

    R = count;11 11

    R = count --; R = count;

    count = count 1; 10 9

    R = --count; Count = count 1;

    R = count;9 9

  • 7/29/2019 Chapter4 Basic C Operators-New

    8/21

    Principles of Programming - NI July2005 8

    Exercise

    #include void main (){int a, b;int sum1, sum2,a1,b1;a = b = 2;

    a1 = ++a; //a=a+1-> increment the value of a before returning the valueb1 = b++; //b=b+1 -> return the current value of b and then increment the value of b

    printf("\nThe value a1 is %d", a1);printf("\nThe value ++a is %d", ++a);

    printf("\nThe value b1 is %d", b1);printf("\nThe value b++ is %d", b++);

    }

  • 7/29/2019 Chapter4 Basic C Operators-New

    9/21

    Principles of Programming - NI July2005 9

    Binary Operators

    C Operation Operator Example:Addition + a + 3

    Subtraction - a - 6

    Multiplication * a * b

    Division / a / c

    Modulus % a % x

    The division of variables of type int will alwaysproduce a variable of type int as the result.

    You could only use modulus (%) operationon int variables.

  • 7/29/2019 Chapter4 Basic C Operators-New

    10/21

    Principles of Programming - NI July2005 10

    Assignment Operators

    Assignment operators are used to combine the'=' operator with one of the binary arithmeticoperators

    In the following slide, All operations starting fromc = 9

    Operator Example EquivalentStatement

    Results

    += c += 7 c = c + 7 c = 16

    -= c -= 8 c = c 8 c = 1

    *= c *= 10 c = c * 10 c = 90

    /= c /= 5 c = c / 5 c = 1

    %= c %= 5 c = c % 5 c = 4

  • 7/29/2019 Chapter4 Basic C Operators-New

    11/21

    Principles of Programming - NI July2005 11

    Precedence Rules

    Precedence rules come into play when there is a mixedof arithmetic operators in one statement. For example:x = 3 * a - ++b%3;

    The rules specify which of the operators will beevaluated first.

  • 7/29/2019 Chapter4 Basic C Operators-New

    12/21

    Precedence of C operators

    Principles of Programming - NI July2005 12

  • 7/29/2019 Chapter4 Basic C Operators-New

    13/21

    Principles of Programming - NI July2005 13

    Precedence Rules cont

    Precedence Order (highest to lowest):For example: 1 + 2 * 3 is treated as 1 + (2 * 3),

    whereas 1 * 2 + 3 is treated as (1 * 2) + 3

    Associativity (order operators of equal

    precedence in an expression are applied, leftto right OR right to left):

    For example: x = y = z = 17 is treated as x = (y

    = (z = 17))

    What about this?X=72 / 2 / 3

  • 7/29/2019 Chapter4 Basic C Operators-New

    14/21

    Excercise

    What is the output / value of the followingstatements:

    X=9-12/3+3*2-1

    Y = 12 * 2 * 2 + 3 / 2

    If (10==10 + 15 && 6 < 10)

    Principles of Programming - NI July2005 14

  • 7/29/2019 Chapter4 Basic C Operators-New

    15/21

    Principles of Programming - NI July2005 15

    Equality and Relational Operators

    Equality Operators:Operator Example Meaning

    == x == y x is equal to y

    != x != y x is not equal to y

    Relational Operators:Operator Example Meaning

    > x > y x is greater than y

    < x < y x is less than y

    >= x >= y x is greater than or equal to y

  • 7/29/2019 Chapter4 Basic C Operators-New

    16/21

    Principles of Programming - NI July2005 16

    Logical Operators

    Logical operators are useful when we want totest multiple conditions.

    There are 3 types of logical operators and theywork the same way as the boolean AND, OR andNOT operators.

    && - Logical AND

    All the conditions must be true for the whole

    expression to be true.Example: if (a == 10 && b == 9 && d == 1)

    means the ifstatement is only true when a ==10 andb == 9 andd== 1.

  • 7/29/2019 Chapter4 Basic C Operators-New

    17/21

    Principles of Programming - NI July2005 17

    Logical Operators cont

    || - Logical ORThe truth of one condition is enough to makethe whole expression true.

    Example: if (a == 10 || b == 9 || d == 1)

    means the ifstatement is true when eitherone ofa, b ordhas the right value.

    ! - Logical NOT (also called logical negation)

    Reverse the meaning of a conditionExample: if (!(points > 90))

    means if points not bigger than 90.

  • 7/29/2019 Chapter4 Basic C Operators-New

    18/21

    Principles of Programming - NI July2005 18

    Conditional Operator

    The conditional operator (?:) is used tosimplify an if/else statement.

    Syntax:Condition ? Expression1 : Expression2

    The statement above is equivalent to:

    if (Condition)

    Expression1

    elseExpression2

  • 7/29/2019 Chapter4 Basic C Operators-New

    19/21

    Principles of Programming - NI July2005 19

    Conditional Operator cont

    Example 1:

    if/else statement:

    if (total > 60)

    grade = Pelse

    grade = F;

    conditional statement:

    total > 60 ? grade = P: grade = F;

    OR

    grade = total > 60 ? P: F;

  • 7/29/2019 Chapter4 Basic C Operators-New

    20/21

    Principles of Programming - NI July2005 20

    Conditional Operator cont

    Example 2:

    if/else statement:

    if (total > 60)

    printf(Passed!!\n);

    else

    printf(Failed!!\n);

    Conditional Statement:

    printf(%s!!\n, total > 60? Passed: Failed);

  • 7/29/2019 Chapter4 Basic C Operators-New

    21/21

    Principles of Programming - NI July2005 21

    SUMMARY

    This chapter exposed you the operators usedin C

    Arithmetic operators

    Assignment operators

    Equalities and relational operatorsLogical operators

    Conditional operator

    Precedence levels come into play when there

    is a mixed of arithmetic operators in onestatement.

    Pre/post fix - effects the result of statement