Students should be able to:
Understand concepts and fundamentals in
expression / operator
Write expression in C programming language
2 Prepared by Norhamreeza Abdul Hamid
Given the following statement:
2x + 3 – z = y
expression
3 Prepared by Norhamreeza Abdul Hamid
Process involves in money withdrawal scenario
balance – drew out money = current balance
Expression in C :
currentBalance = withdrawMoney – balance;
4 Prepared by Norhamreeza Abdul Hamid
Operator
example
x+3 - z
Operand
arithmetic
Consists of
relational
logical
5 Prepared by Norhamreeza Abdul Hamid
Known as
Mathematic Expression
using
Arithmetic Operator
Unary Operator Binary Operator Ternary Operator
Represents by Represents by
Operator Meaning
Operator Meaning
* multiply
- Negative
/ divide
+ Positive
% modulus
-- Decrement
+ add
++ increment
- subtraction
6 Prepared by Norhamreeza Abdul Hamid
Arithmetic operators can be used with any numeric type.
They operate on numbers and the result is a number.
The type of the result depends on the types of the operands.
If the types of the operands differ (e.g.: an integer added to
a floating point number), one is “promoted” to other.
The “smaller” type is promoted to the “larger” one
char int float double
Result of an operator depends on the types of operands
If both operands are int, the result is int
If one or both operands are double/float, the result is
double
7 Prepared by Norhamreeza Abdul Hamid
Unary Operator Operates for one operand
example
Computer memory cell
a = -20
a -20
b = +15
b +15
Increment Decrement
Prefix ++h --h
Postfix h++ h--
8 Prepared by Norhamreeza Abdul Hamid
9 Prepared by Norhamreeza Abdul Hamid
10 Prepared by Norhamreeza Abdul Hamid
EXERCISE 1
int A = 5;
++A;
printf(“%d”,A); // output??
A--;
printf(“%d”,A); // output??
A++;
printf(“%d”,A); // output??
11 Prepared by Norhamreeza Abdul Hamid
EXERCISE 2
int B;
B = 2;
printf(“%d”, 3 + --B); // output??
printf(“%d”, B); // output??
B = 9;
printf(“%d”, 3 + B--); // output??
printf(“%d”, B); // output??
12 Prepared by Norhamreeza Abdul Hamid
EXERCISE 3
Given the value of num1 = 8. Determine the
value of num2 after the execution for each of the
following statements:
num2 = num1++ - 2;
num2 = num1;
num2 = ++num1 – 3;
num2 = num1-- +1;
num2 = num1;
13 Prepared by Norhamreeza Abdul Hamid
Located between constants or variables
Binary operator
or both combination
example
A + z
Operator
Operand
14 Prepared by Norhamreeza Abdul Hamid
Multiplication Use symbol " * "
example
A * z
Operator
Operand
Mathematic Arithmetic Expression
2x + y 2*x+y
15 Prepared by Norhamreeza Abdul Hamid
Division Use symbol " / "
example
A / z
Operator
Operand
Mathematic Arithmetic Expression
2÷y 2/y
16 Prepared by Norhamreeza Abdul Hamid
int x = 5;
float y = 2.0, result; The result of the “double
result = x / y; division” is 2.5.
printf(“%f”, result);
Before the division
result = 5.0 / 2.0 process, 5 is promoted
result = 2.5 from integer 5 to float
5.0
17 Prepared by Norhamreeza Abdul Hamid
int x = 5, y = 2, result;;
result = x / y; The result of the “integer
division” is 2.
printf(“%d”, result);
There is no promotion
result = 5 / 2 occurred. Both operands
result = 2 are the same type.
18 Prepared by Norhamreeza Abdul Hamid
Modulus Use symbol " % "
example
A % z
Operator
Operand
Return a balance when 2 numbers is divided
Can only be used with an integer variable
19 Prepared by Norhamreeza Abdul Hamid
int A, B;
float C;
A = 2;
B = 5;
C = 2.4;
B % A; Valid answer is 1
C % A; Invalid! C is float
20 Prepared by Norhamreeza Abdul Hamid
Output for
B/A=2
int A, B;
2
float C; 2 5
A = 2; 4
B = 5; 1
Output for
B % A;
B%A=1
21 Prepared by Norhamreeza Abdul Hamid
Has two parts and requires three
Ternary operator
operands.
2nd operand
is a value
example
(a > 2) ? 1 : 0
1st operand 3rd operand is
is a condition another value
22 Prepared by Norhamreeza Abdul Hamid
Store value or result of process to variable
Use operator symbol “=”
Assignment statement
23 Prepared by Norhamreeza Abdul Hamid
variable = value;
Format
Syntax
variable = constant; or variable = variable;
variable = expression;
1. average = (6 + 4) / 2; average 5
2. grossSalary = 1800; grossSalary 1800
nettSalary = grossSalary + 300; nettSalary 2100
3. price = 55.00; price 55.00
pay = price; pay 55.00
24 Prepared by Norhamreeza Abdul Hamid
int a = b = c = d = e = 250;
int b = 2, number = 0, total = 0, average = 3;
int age = workhour = 0;
25 Prepared by Norhamreeza Abdul Hamid
26 Prepared by Norhamreeza Abdul Hamid
Operator Expression Meaning
+= total + = 300 total = total+ 300
-= total - = count+ 300 total = total - (count + 300)
*= total *=300 total = total * 300
/= total /= count– 10 total = total / ( count –10)
%= total % = 7 total = total % 7
27 Prepared by Norhamreeza Abdul Hamid
To determine the order in which different operators.
The higher precedence will done first followed by the lower
precedence.
Compiler will follows the following precedence to execute the
arithmetic expression based on priority.
Operator Priority
() First
++, -- Second
*, /, % Next
+, - Last
If the operators have the same precedence, then follows the rules
of associativity
28 Prepared by Norhamreeza Abdul Hamid
Example
1. 5 + 2 * 6 – 4 / 2 2. 3 * 4 / 2 + ( 3 – 1 )
5 + 12 - 4 / 2 3*4/2 + 2
5 + 12 - 2 12 / 2 + 2
17 - 2 6+2
15 8
29 Prepared by Norhamreeza Abdul Hamid
Example
3. Prefix unary arithmetic expression
int kira = 5, nilai_pertama = 10;
nilai_kedua = 5 * --kira + nilai_pertama;
printf(“%d %d”, kira, nilai_kedua);
4 30
30 Prepared by Norhamreeza Abdul Hamid
Example
3. Prefix unary arithmetic expression
int kira = 5, nilai_pertama = 10;
nilai_kedua = 5 * kira-- + nilai_pertama;
printf(“%d %d”, kira, nilai_kedua);
4 35
31 Prepared by Norhamreeza Abdul Hamid
Associativity can be form either the left or the right
Left associativity evaluates expression by starting
on the left and moving to the right
Right associativity –from right to the left
Associativity is used only when the operators all
have the same precedence level
Example: Associativity from left to right
3*8/4%4*5
((((3 * 8) / 4) % 4) * 5)
32 Prepared by Norhamreeza Abdul Hamid
Compiler will follows the following associativity to execute the
arithmetic expression if the operators have the same precedence.
Operator Associativity
() Left to right
+ (unary), - (unary), ++, --, ! Right to left
*, /, % Left to right
+, - Left to right
<, <=, >, >= Left to right
==, != Left to right
&& Left to right
|| Left to right
?: Right to left
=, +=, -=, *=, /=, %= Right to left
33 Prepared by Norhamreeza Abdul Hamid
Example: Left associativity
3*8/4%4*5
34 Prepared by Norhamreeza Abdul Hamid
Example: Right associativity
a += b *= c -= 5
35 Prepared by Norhamreeza Abdul Hamid
Header file designed for basic mathematical
operations (math.h)
Syntax #include <math.h>
Below are lists of some common math
functions:
Function Description
sqrt(x) compute the non-negative square root of x
pow(x,y) computes x raised to the power of y
cos(x) compute the cosine of x (measured in radians)
sin(x) computes the sine of x (measured in radians)
tan(x) compute the tangent of x (measured in radians)
36 Prepared by Norhamreeza Abdul Hamid
Example
#include <stdio.h>
#include <math.h>
int main()
{
int x = 25, y;
y = sqrt(x);
printf(“%d”, y);
}
5
37 Prepared by Norhamreeza Abdul Hamid
1. Convert the following mathematic expression to a valid
arithmetic expression :
a) b = 3 + b b) x = (a – b)(a – c2)
a+4
c) d = (3e – d) - ( 4 – 3c3 ) d) r = 2s + 3(s – 9)
x–9 4y s
2. Given a= 3, b = 5, c=1. What is the output of the following
expression?
a. ( 6 * c – 6 / a) - b b. (5 * c) +( a* b / b)
c. ++a d. c + a * c / (3 * c)
38 Prepared by Norhamreeza Abdul Hamid
3. Assume i, j and k are integer variables with i = 5 and j = 3.
Determine the value for each of the following statement:
a) k = j++; b) k = i * j--;
c) k = j + i * j++; d) k = ++j;
e) k = i * --j; f) k = 27 / j++ - 16 % i;
39 Prepared by Norhamreeza Abdul Hamid
use
Relational Operator
Combination of more than one statement
Consists of
Produce
40 Prepared by Norhamreeza Abdul Hamid
Operator Description
== Equal to
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
!= Not equal
41 Prepared by Norhamreeza Abdul Hamid
Notes:
a, b and c are variables.
Replace with the given values
int a=6, b =1, c = -2;
1) a+ b == c 2) a != b
6 + 1== -2 6 != 1
7 == -2
Answer: 0 (False) Answer : 1 (True)
42 Prepared by Norhamreeza Abdul Hamid
int a=6, b =1, c = -2;
3) b < c 4) b + c <= a
1 < -2 1 + -2 <= 6
-1 <= 6
Answer: 0 (False) Answer : 1 (True)
43 Prepared by Norhamreeza Abdul Hamid
Notes:
Relational operator has less
priority than other operators.
int a=10, b = 3, c = 7; Start evaluating from left to right.
(a+b >= 3*c)==( a != 2*c+b)
(10+3 >= 3*7)==(a != 2*c+b)
(13 >= 21)==(10 != 14+3)
(13 >= 21)==(10 != 17)
0 == 1
0 (false)
44 Prepared by Norhamreeza Abdul Hamid
An Example program which uses Relational Expression
#include <stdio.h>
int main()
{
int age;
printf(“Please enter your age: ”);
scanf(“%d”, &age);
if (age > 21)
printf(“You are qualified to vote”);
return 0;
}
45 Prepared by Norhamreeza Abdul Hamid
use
Logical Operator
Combination of one or more expressions
Consists of
Produce
46 Prepared by Norhamreeza Abdul Hamid
Operator Description
&& AND
|| OR
! NOT
Logical operator && and || is used between 2 or
more relational expression
47 Prepared by Norhamreeza Abdul Hamid
Logical operator truth table for AND
AND (&&) Logical Operator Result
False AND False False
Value 0 1
False AND True False
0 0 0
True AND False False
1 0 1
True AND True True
48 Prepared by Norhamreeza Abdul Hamid
Logical operator truth table for OR
OR (||) Logical Operator Result
False OR False False
Value 0 1
False OR True True
1 0 1
True OR False True
1 1 1
True OR True True
49 Prepared by Norhamreeza Abdul Hamid
Logical operator truth table for NOT
NOT (!)
Logical Operator Result
Value Result
False OR False False
!0 1
False OR True True
!1 0
50 Prepared by Norhamreeza Abdul Hamid
Evaluate the following logical expression:
a) (2 < 5 ) && ( 5 < 10) b) (7 % 2 < 2) || ( 2 * 3 == 6)
1 && 1 (1 < 2) || (6 == 6)
1 1 || 1
1
51 Prepared by Norhamreeza Abdul Hamid
Evaluate the following logical expression:
Given a = 3, b = 4;
c) !((5 * b <= 23 - a )) d) ! ((b +3 != 8) && ( 3 * a < 2))
!((5 * 4 <= 23 – 3)) !(( 7 != 8 ) && ( 9 < 2 ))
!(20 <= 20) !( 1 && 0 )
!(1) ! ( 0)
0 1
52 Prepared by Norhamreeza Abdul Hamid
An Example program which using Logical Expression
#include <stdio.h>
int main()
{
int marks;
printf(“\nPlease enter your marks >> ”);
scanf(“%d”, &marks);
if (marks >= 85 && marks <= 100)
printf(“\nGred A”;
else if (marks >= 80 && marks <= 84)
printf(“\nGred A-”;
return 0;
}
53 Prepared by Norhamreeza Abdul Hamid
1. Given with i = 2, j = 5 and k = 15. Evaluate each of the following expression:
a) i>j–k g) k == j + i * j
b) i != k h) k <= k / j
c) (i >= 1) && (j == 5) i) (j < i) || (k > j)
d) ! (i > j) j) (i > 0) && (j < k) || (k < i)
e) i<j<k k) i * k < k / j
f) (i < j) && (j < k) l) i–j>k
2. Complete the following statements with suitable logical expression.
int angka1, angka2;
if (angka1 is less than or equal to angka2)
printf(“%d is less than or equal to %d”, angka1, angka2);
54 Prepared by Norhamreeza Abdul Hamid
55 Prepared by Norhamreeza Abdul Hamid