Computer Programming DAM 23603: Expressions and Operators
Computer Programming DAM 23603: Expressions and Operators
Computer Programming DAM 23603: Expressions and Operators
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)
#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
• 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)
• Division calculations
ARITHMETIC OPERATORS
(Binary Arithmetic Operators)
• Modulos calculations
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)
• 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
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
OR || exp1 || exp2
NOT ! !exp1
Logic Operator
Assessing the expression for the operator &&:
0 !(0) 1
Logic Operator
Example:
Expression What It Evaluates To