Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4: Expression and Operator

Similar presentations


Presentation on theme: "Chapter 4: Expression and Operator"— Presentation transcript:

1 Chapter 4: Expression and Operator

2 Expression: Definition
An expression is any legal combination of symbols that represents a value. Each programming language and application has its own rules for what is legal and illegal. For example, in the C language x+5 is an expression Every expression consists of at least one operand and can have one or more operators. Operands are values, whereas operators are symbols that represent particular actions. In the expression In x + 5, x and 5 are operands, and + is an operator.

3 Operator: Definition An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators − Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Assignment Operators Increment/decrement operator Misc Operators

4 Arithmetic Operator Operator that performs addition, subtraction, multiplication and division C programming has following binary arithmetic operator Arithmetic Unary operator are + and -

5 Binary Arithmetic Operator: Example

6 Unary Arithmetic Operator: Example

7 Arithmetic Operator precedence and associativity
If more than one operators are involved in an expression, C language has a predefined rule of priority for the operators. This rule of priority of operators is called operator precedence. i + j * k equivalent to i + (j * k) or (i + j) * k

8 Arithmetic Operator Precedence
Highest (unary) * / % Lowest (binary) Listed on same line same precedence

9 Arithmetic Operator Associativity
When operator with same precedence came then associativity matters Left associative if it groups from left to right. Binary arithmetic operator are left associative Right associative if it groups from right to left. The unary operator (+, -) are right associative

10 Assignment Operator Assigns values from left to right variable
= is simple assignment operator If different data type then left operand data type is converted to right.

11 Assignment Operator Contd..
Assignment operator can be chained together Eg. i=j=k=0; = is right associative thus- i=j=k=0 is equivalent to i=(j=(k=0)); What is assigned to f? int I; float f; f=i=3.2f

12 Compound Assignment Compound assignment allows to shorten statement
Common compound assignment operator are Has same precedence as =

13 Increment and Decrement Operator
Increases and decreases value by 1. Can simple done by using compound statement i=i+1;  i+=1; j=j-1;  j+=1; But C provided ++ and -- operator which shorten even further Can be used on two way with two different meaning prefix and postfix

14 Prefix and Postfix increment / decrement
Eg. Same for -- operator

15 More Example What are value of i, j and k ?

16 Expression Evaluation

17 Expression Statement Any expression by appending semicolon is turned into statement i++; i+j; (do nothing statement)

18 Other Operator Relational and logical operator


Download ppt "Chapter 4: Expression and Operator"

Similar presentations


Ads by Google