chapter 3 expression and operators

18
COMPUTER PROGRAMMING DAE 20102 CHAPTER 3: Expressions And Operators

Upload: jay-leong

Post on 20-Jan-2015

36 views

Category:

Education


3 download

DESCRIPTION

CHAPTER 3, COMPUTER PROGRAMMING (DAE 20102)

TRANSCRIPT

Page 1: Chapter 3 expression and operators

COMPUTER PROGRAMMING

DAE 20102CHAPTER 3:

Expressions And Operators

Page 2: Chapter 3 expression and operators

OPERATORS(Operators and Symbols)

Operator SymbolArithmetic +,-,*,/,%Relational <,<=,>,>=,==,!=

Logic &&,||,!

• Symbols like +,-,*,/,<,> are known as operators.

• Data stored in memory can be modified using operators.

• The operators are divided into three (3) distinct types.

Page 3: Chapter 3 expression and operators

ARITHMETIC OPERATORS

Arithmetic operators in C are divided into two (2):

• Unary

• Binary

Page 4: Chapter 3 expression and operators

ARITHMETIC OPERATORS(Unary Operators Symbols)

Operator Function

+ Positive operator

- Negative operator

++ Increment operator

-- Decrement operator

! Not operator

& Address operator

* Value of address operator

• Unary operators are fixed to one (1) variable only.

Page 5: Chapter 3 expression and operators

EXPRESSIONS

Content of a Before

Expressions Value of expressions

Content of a After

10 a++ (a=a+1) 10 11

10 a-- 10 9

10 ++a (a=a+1) 11 11

10 --a 9 9

Page 6: Chapter 3 expression and operators

EXPRESSIONS

#include <stdio.h>int main(void) { int a= 1, b=1, c=3, d=1;

c--;printf(“%d %d”, 4-d++, c);printf(“%d %d %d %d \n”, --a, b++, c--, d);

return 0;}

Page 7: Chapter 3 expression and operators

EXPRESSIONS

Expression Value of x Value of y

y+=x;y=y+x;

5 12

y-= x++; 6 2

y*= ++x * 2; 6 84

• Assume that int x = 5; int y = 7;

Page 8: Chapter 3 expression and operators

EXPRESSIONS

Expression Value of x Value of y

y+=x; y+=xy = y + x = 7 + 5 = 12

5 12

• Assume that int x = 5; int y = 7;

Page 9: Chapter 3 expression and operators

EXPRESSIONS

Expression Value of x Value of y

y-= x++; y-=x++y = y – x++ = 7 – 5 = 2

6 2

• Assume that int x = 5; int y = 7;

Page 10: Chapter 3 expression and operators

EXPRESSIONS

Expression Value of x Value of y

y*= ++x * 2; y*=++x * 2y = y * (++x) * 2 = 7 * (6) * 2 = 42 * 2 = 84

6 84

• Assume that int x = 5; int y = 7;

Page 11: Chapter 3 expression and operators

ARITHMETIC OPERATORS(Binary Operators Symbols)

Operator Function

+ Addition operator

- Subtraction operator

* Multiplication operator

/ Division operator

% Remainder operator (int only)

• Operands that are fixed with arithmetic operators must be of numeric types.

• Operators is put in between two (2) operands.

Page 12: Chapter 3 expression and operators

ARITHMETIC OPERATORS(Binary Arithmetic Operators)

Value a Value b Integer division Real division

10 3 10/3 = 3 10.0/3.0=3.333333

5 10 5/10 = 0 5.0/10.0=0.500000

6 6 6/6 = 1 6.0/6.0=1.000000

• Division calculations

Page 13: Chapter 3 expression and operators

ARITHMETIC OPERATORS(Binary Arithmetic Operators)

Operation Modulos result Remainder

5 % 3 1 2

7 % 2 3 1

12 % 2 6 0

• Modulos calculations

Page 14: Chapter 3 expression and operators

ARITHMETIC EXPRESSIONS(Precedence levels)

• Combining one or more arithmetic operations.• Precedence level will determine the arithmetic operation

sequence in solving the expression.

Precedence level Operation

High ()

++,--

*,/,%

+,-

+=,-=,*=,/=,%=

>,<,<=,>=

Low ==,!=

Page 15: Chapter 3 expression and operators

ASSIGNMENT STATEMENT

• Assignment of data to the variable can be written as:Variable = expression;

e.g:-Berat_Kereta = 85;Diameter_Tayar = 18;Horse_Power = Diameter_Tayar * ( 12 +

Berat_Kereta);

• For the above statement, the left part can only be a variable. On the right, it can be made up of a combination of variables and constants.

Page 16: Chapter 3 expression and operators

ASSIGNMENT STATEMENT(Compound Assignment Statement)

Operation Example Meaning

+= val += 5; val = val + 5

-= Val -= 5; val = val – 5

*= val *= 5; val = val * 5

/= val /= 5; val = val / 5

%= val %= 5; val = val % 5

• Modify a variable value where the variable original value is added/minus/multiply by another value and is assigned back to the original variable.

• e.g:price = price – discount;

Page 17: Chapter 3 expression and operators

RELATIONAL OPERATORS

Operator Meaning

< Less than

<= Less than or equal to

> Greater than

>= Greater than or equal to

== Equal to

!= Not equal to

• Used to do comparisons.

Page 18: Chapter 3 expression and operators

LOGIC OPERATOR

Operator Function Meaning

&& And It is true if and only if both the expressions are true

|| Or It is true if one or both the expressions are true

! Not It is true if the expression is false and vice versa

• Useful to do complex comparisons to make decisions.