0% found this document useful (0 votes)
42 views41 pages

Computer Programming DAM 23603: Expressions and Operators

The document discusses various operators used in computer programming. It describes: 1) Arithmetic operators like addition, subtraction, multiplication and division that are used to modify operands. These include unary operators like increment/decrement and binary operators that operate between two operands. 2) Relational operators like equal, greater than, less than that are used to compare operands and return true or false. 3) Logic operators like AND, OR, NOT that are used to combine relational expressions and make complex comparisons. The document provides examples of expressions using these various operators and how they are evaluated. It also discusses precedence rules when combining multiple operators in an expression.

Uploaded by

Hamizan Azli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views41 pages

Computer Programming DAM 23603: Expressions and Operators

The document discusses various operators used in computer programming. It describes: 1) Arithmetic operators like addition, subtraction, multiplication and division that are used to modify operands. These include unary operators like increment/decrement and binary operators that operate between two operands. 2) Relational operators like equal, greater than, less than that are used to compare operands and return true or false. 3) Logic operators like AND, OR, NOT that are used to combine relational expressions and make complex comparisons. The document provides examples of expressions using these various operators and how they are evaluated. It also discusses precedence rules when combining multiple operators in an expression.

Uploaded by

Hamizan Azli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 41

COMPUTER PROGRAMMING

DAM 23603
CHAPTER 4:
Expressions And Operators
INTRODUCTION
 Consider the following expression :
y = x + 3
a > b
 a, b, x, y and 3 are an operand (consists of
constants and variables)
 Symbol =, + and > are operators
OPERATORS
(Operators and Symbols)

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

• Data stored in memory can be modified using operators.


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

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


• Unary
• Binary
ARITHMETIC OPERATORS
(Unary Operators Symbols)

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


ARITHMETIC OPERATORS
(Unary Operators Symbols)

Symbol Example Note


+ +2 No. +ve; default
- -30 No. -ve
++ ++value Prefix: before execution
value++ Postfix: after execution

-- --no Prefix:before execution


no-- Postfix: after execution
ARITHMETIC OPERATORS
(Unary Operators Symbols: Expressions)
ARITHMETIC OPERATORS
(Unary Operators Symbols: 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;
}
Example of using sum

POSTINCREMENT PREINCREMENT

int count = 5; int count = 5;


: :
printf(“%d”, count++ ); printf(“%d”, ++count );
printf(“%d”, count ); printf(“%d”, count );
   

Value are 5 6 Value are 6 6


Example by using subsract
POSTDECREMENT PREDECREMENT
value = 5; a = 10; value = 5; a = 10;
: :
b = 5 * --value + a; b = 5 * value-- + a;
printf(“%d %d”, value, b); printf(“%d %d”, value, b);
   
Value are 4 30 Value are 5 35
   
b= 5 * --value + a; b= 5 * value-- + a;
└─┬─┘ └─┬─┘
(a) 4 (a) 5
└──┬──┘ └──┬──┘
(b) 20 (b) 25
└───┬──┘ └───┬──┘
(c) 30 (c) 35
 
ARITHMETIC OPERATORS
(Unary Operators Symbols: Expressions)

• Assume that
int x = 5;
int y = 7;
ARITHMETIC OPERATORS
(Unary Operators Symbols: Expressions)

• Assume that
int x = 5;
int y = 7;
ARITHMETIC OPERATORS
(Unary Operators Symbols: Expressions)

• Assume that
int x = 5;
int y = 7;
ARITHMETIC OPERATORS
(Unary Operators Symbols: Expressions)

• Assume that
int x = 5;
int y = 7;
ARITHMETIC OPERATORS
(Binary Operators Symbols)

• Operands that are fixed with arithmetic operators must


be of numeric types.
• Operators is put in between two (2) operands.
ARITHMETIC OPERATORS
(Binary Operators Symbols)

Operator Symbol Action Example

Addition + Adds two operands x+y

Subtraction - Subtracts the second operand from the first x-y


operand
Multiplication * Multiplies two operands x*y

Division / Divides the first operand by the second x/y


operand
Modulus % Gives the remainder when the first operand is x%y
divided by the second operand (integers only)
ARITHMETIC OPERATORS
(Binary Operators Symbols)
 Issue:
 Complex Expression
 Example:
a * b + c
a * ( b + c)
 Different data types
 Example:
int a, b, c; float d;
c = a/b;  what are the value?
d = a/b;  what are the value?
ARITHMETIC OPERATORS
(Binary Arithmetic Operators)

• Division calculations
ARITHMETIC OPERATORS
(Binary Arithmetic Operators)

• Modulos calculations
ARITHMETIC EXPRESSIONS
(Precedence levels)

• Combining one or more arithmetic operations.


•Precedence level will determine the arithmetic operation
sequence in solving the expression.
ARITHMETIC EXPRESSIONS
(Precedence levels)

 Precedence
Precedence Operation
1 ()
2 *,/,%
3 +,-
 Example:
Value1 = i + j * l / k;
EXAMPLE:
5+ 2*6 – 4 / 2
└─┬─┘
5 + 12 – 4 / 2
└─┬┘
5 + 12 – 2
└─┬─┘
17 – 2
└─┬┘
15
ASSIGNMENT STATEMENT
ARITHMETIC EXPRESSIONS
(Precedence levels)

• 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.
CAST OPERATOR
 Cast
 Required to convert the data type in a
temporary storage
 Using a cast operator
 Example: double a,b,c;
c = a % b; //ralat!
c = (int) a % (int) b;

Cast Operator
Example of casting:
double value1 = 9.5, value3;
int value2;
:
value2 = value1 % 3;
value3 = (value1 * 10 % 20 ) / 2.0

value2 = ( int ) value1 % 3;


value3 = (( int ) (value1 * 10 ) % 20 ) / 2.0;
Math Library Function
 Use for the difficult mathematical formula,
 Example:
luas
jejari 

 Header file is: math.h
ceil(x), floor(x), abs(x), fabs(x),
sqrt(x), pow(x,y), cos(x), sin(x),
tan(x), exp(x), log(x), log10(x)
Math Library Function
 Example:
Instruction Maths formula

A = pow(6,100); A = 6100

B = pow(x,3); B = x3
(
or instruction C:
B = x * x * x; )
C = sqrt(144); C  144
Relational Operator
 Use to compare
 Example:
 ‘Does A larger than B?’
 ‘Does Y is 0 value ?’
 Value that will return back will be 1 (TRUE) or 0
(FALSE)
RELATIONAL OPERATORS

• Used to do comparisons.
Relational Operator
Operator Symbol Question Asked Eg

Equal == Is operand1 equal to operand2? x == y

Greater than > Is operand1 greater than operand2? x>y

Less than < Is operand1 less than operand2? x<y

Greater than or >= Is operand1 greater than or equal to x >= y


equal to operand2?
Less than or equal <= Is operand1 less than or equal to x <= y
to operand2?
Not equal != Is operand1 not equal to operand2? x != y
Relational Operator
 Example:
Expression How It Reads What It Evaluates
To
5 == 1 Is 5 equal to 1? 0 (false)

5>1 Is 5 greater than 1? 1 (true)

5 != 1 Is 5 not equal to 1? 1 (true)

(5+10) == (3*5) Is (5+10) equal to 1 (true)


(3*5)?
Logic Operator
 Used to combine several well-expression
contains relational operators
 Useful for making comparisons for
complex decision making
 Example:
 ‘Does Y is greater than 0 and Y is smaller than
10?’
 0 < Y < 10
LOGIC OPERATOR

• Useful to do complex comparisons to make decisions.


Logic Operator
 Value to return: 1 (true), 0 (false)
 Example:

Operator Symbol Example

AND && exp1 && exp2

OR || exp1 || exp2

NOT ! !exp1
Logic Operator
 Assessing the expression for the operator &&:

Expression1 Expression2 Result


1 1 1
1 0 0
0 1 0
0 0 0
Logic Operator
 Assessing the expression for the operator or
||:
Expression1 Expression2 Result
1 1 1
1 0 1
0 1 1
0 0 0
Logic Operator
 Assessing the expression for the
operator !:
Real Combination
Expression Operator! Result
1 !(1) 0

0 !(0) 1
Logic Operator
 Example:
Expression What It Evaluates To

(5 == 5) && (6 != 2) True (1), because both operands are


true
(5 > 1) || (6 < 1) True (1), because one operand is true

(2 == 1) && (5 == 5) False (0), because one operand is false

!(5 == 4) True (1), because the operand is false


ASSIGNMENT STATEMENT
(Compound Assignment Statement)

• 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;
Compound expression vs simple
expression
Compound expression Simple expression
x *= y x=x*y
x /= y x=x/y
x %= y x=x%y
x+=y ?
? x = x * (y+3)
x /= y-1 ?
-END-
THANK YOU

You might also like