Your SlideShare is downloading. ×
  • Like
Chapter 3 dti2143
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Chapter 3 dti2143

  • 616 views
Published

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
616
On SlideShare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
68
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. EXPRESSIONS AND OPERATORS
    CHAPTER 3
    DTI 2143
    1
  • 2. CONTENTS
    Arithmetic Operators
    Assigning Mathematical Operator Combinations
    Unary Sign Operators
    Increments And Decrement Operator
    Conditional Operator
    Logical Expressions
    Logical Operator
    2
    2
    CHAPTER 4
  • 3. INTRODUCTION
    In any programming languages, we need operators to construct statements to do an operation and calculations.
    In this chapter, you will be introduced to several operators such as:
    Unary Operators
    Binary Operators
    Ternary Operators
    Operator can be applied to variables or any elements in an expression to produce some kind of computation or action.
    3
  • 4. ARITHMETIC OPERATORS
    We can form more complicated arithmetic expressions by using arithmetic operators.
    There are two types of arithmetic operators in c such as:
    Unary arithmetic operators
    • Required one operand.
    Binary arithmetic operators
    • Required two operand.
    4
  • 5. Examples of Unary arithmetic operators
    Binary arithmetic operators
    5
  • 6. Example 1
    #include <stdio.h>
    #include <conio.h>
    int main(void)
    {
    int num= 20;
    int val=30;
    num=val+5; /* num=30+5*/
    val=num-val; /*val=35-30*/
    num=num+2; /*num=35-5*/
    val=30/7; /*val=30/7*/
    getch();
    return 0;
    }
    6
  • 7. Example 2
    #include <stdio.h>
    #include <conio.h>
    int main(void)
    {
    int num= 20;
    printf (“%dnn”, num);
    int val=30;
    printf (“%dnn”, val);
    num=val+5;
    printf (“%dnn”, num);
    val=num-val;
    printf (“%dnn”, val);
    num=num+2;
    printf (“%dnn”, num);
    val=30/7;
    printf (“%dnn”, val);
    getch();
    return 0;
    }
    7
  • 8. Example 3
    #include <stdio.h>
    #include <conio.h>
    int main(void)
    {
    int a,b,c,d,result;
    a=10;
    b=20;
    c=15;
    d=8;
    result=a*b/(-c*31%13)*d;
    printf ("%d",result);
    getch();
    return 0;
    }
    8
  • 9. How to Solves Example 3?
    • The expression will evaluated 6 steps
    a=10,b=20,c=15,d=8,e=40,
    a * b / (-c * 31 % 13 )* d
    9
  • 10. Example 3
    #include <stdio.h>
    #include <conio.h>
    int main(void)
    {
    int num =5;
    num -=3;
    num +=4;
    printf ("%d", num);
    getch();
    return 0;
    }
    10
  • 11. Example 4
    total = total / 2;
    bonus=bonus+800;
    balance =balance- sum + tax
    11
  • 12. INCREMENTS AND DECREMENT OPERATOR
    • In C, we have two special operators for incrementing or decrementing a variable by one.
    • 13. The operators are:
    Unary Increment operators : ++
    ++i (prefix)
    i++ (postfix)
    Unary Decrement Operators : --
    --i (prefix)
    i-- (postfix)
    12
  • 14. Unary Increment operators : ++
    • To increment a variable by 1
    • 15. Also used as counter in loop structure
    • 16. Eg:i=i+1; or
    • 17. i +=1;
    Unary Decrement Operators : --
    • To decrement a variable by 1
    • 18. Eg:i=i-1 or
    • 19. i -=1;
    13
  • 20. INCREMENTS OPERATOR
    /*Output : 1*/
    /*Output : 1*/
    14
  • 21. LOGICAL EXPRESSIONS
    • C provides logical operators that are used to combine expressions into logical expressions.
    15
  • 22. RELATIONAL AND EQUALITY OPERATOR
    16
  • 23. Example 1
    a = 6;
    b = 1;
    c = - 2;
    1
    1
    0
    0
    17
  • 24. LOGICAL OPERATOR
    • The purpose of logical operator is to combine and check the results of logical expressions.
    18
  • 25. EXAMPLE 1
    0(FALSE)
    1(TRUE)
    0(FALSE)
    19
  • 26. EXAMPLE OF QUESTIONS
    1) Given:
    intval, x = 4, y = 3, z = 10;
    val =(x+y>=3*z) == (x!=3*z+y);
    The value of val is: 
    1
    0
    10
    17
    20
  • 27. 2) Given:
    int val, a = 1, b = 2, c = 3, d = 4;
    val = d+a%c*b;
    The value of val is:
    6
    5
    0
    1
    21
  • 28. 3)
    int cost = 2;
    int price = 5;
    cost += ++price * 2;
     
    What is answer for cost?
    22
  • 29. 4)
    Assume count = 21.
    What is the current value of
    count at each of these statements?
     
    a) count-- =……….
    b) ++count =……….
    23
  • 30. 5)
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
    int n01=39,n02=100;
    printf("%d",++n01);
    printf("n%d",n01--+2);
    printf("n%d",(--n01)+(++n02));
    printf("n%d",n01);
    getch ();
    return 0;
    }
  • 31. 6)
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
    int x=6,y=6;
    printf("%d",x++);
    printf("%d",x);
    y--;
    printf("%d %d",x,y);
    getch ();
    return 0;
    }