BIT10303 : Computer
Programming
Chapter 3 : Expressions and Operations
Objective
• At the end of this chapter, student should be able to understand and
apply
• Understand and implement the basic structure of computer programming.
• Write a computer program using C programming language.
• Convert algorithm into computer program.
Chapter : 3
• 3. EXPRESSIONS
Aim
To give understanding on:
AND OPERATIONS ▪Expression and operator concept
• Arithmetic operation ▪math.h and stdlib.h built-in function
• Relational operation
O
• Logical Expression
bjective
Students should be able to:
▪understand concepts and fundamentals in
expression/ operator.
▪write expression in C programming language
Introduction to Expression
Given the following statement
:
Example:
2x + 3 – z = y Process involves in money withdrawal scenario
balance – drew out money = current balance
expression
Expression in C :
current_balance =balance-drew_out_money;
4
Introduction to Expression
What is Expression?
Combination of more than one variable or constant
Expression (operand) which separated by operator
example Operator
x + 3 - z
Operand
Consists of arithmetic
relational
logical
5
❑Arithmetic operation
❑Relational operation
❑Logical Expression
Arithmetic Expression
Known as Mathematic Expression
Arithmetic Expression
using
Arithmetic Operator
Represents by
Represents by
Unary operator Binary operator
Operator Meaning Operator Meaning
- negative * multiply
+ positive / divide
-- decrement + add
++ increment - subtraction
% modulus
7
Arithmetic Expression
Unary Operator
Unary Operator Operates for one operand
example Computer memory cell
a = -20; a -20
b = +15; b +15
Increment Decrement
prefix ++c; --c;
postfix c++; c--;
8
Arithmetic Expression
Unary Operator
Example 1: A 56
int A = 5;
++A; 6
5
printf(“%d”, A); output ? 6
A--; 6
printf(“%d”,A); output ? 4
6
A++;
printf(“%d”,A); output ?
9
Arithmetic Expression
Unary Operator
Example 2:
8 int A ;
5
9
A=6;
5 printf(“%d”,3 + --A); output ? A=(6-1)
printf(“%d”, A); output ? =5 6 5
A=6;
printf(“%d”, 3 + A--); output ?
printf(“%d”, A); output ?
10
Arithmetic Expression
Unary Operator
num1
8 9 10 9
Example 3: num2
11
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;
6 6
num2 = ++num1 – 3;
8 9
6 num2 = num1-- +1; 7
9 11
11
Arithmetic Expression
Binary Operator
Located between constants or
Binary Operator
variables or both combination
example
A + z
operator
operand
12
Arithmetic Expression
Binary Operator
Multiplication Use symbol “ * ”
example
A * z
operator
operand
Mathematic Arithmetic Expression
2x + y 2*x+y
13
Arithmetic Expression
Binary Operator
Divide Use symbol “/”
example
A / z
operator
operand
Mathematic Arithmetic Expression
2÷y 2/y
14
Arithmetic Expression
Binary Operator
Modulus Use symbol “%”
example
A % z
operator
operand
Return a balance when 2 numbers is divided
Can only be used with an integer variable
15
Arithmetic Expression
Binary Operator
Example:
int A, B;
float C;
A= 2;
B=5;
C= 2.4;
Valid! Answer is 1
B% A;
Invalid! C is float
C % A;
16
Arithmetic Expression
Assignment Statement
▪Used to store value/result of process to a variable
▪Use operator symbol =
Assignment statement
Double assignment Compound assignment
statement statement
17
Arithmetic Expression
Assignment Statement
▪ Format /sintax :
variable = value;
variable = constant; or variable = variable;
variable = expression;
▪Example :
1.average= ( 6 + 5) * 4; average 44
1500
2.grossSalary = 1500; grossSalary
4 nettSalary = grossSalary + 200; 1700
nettSalary
3.price= 50.00; price 50.00
5 pay = price;
` pay 50.00
18
Arithmetic Expression
Compound Assignment Statement
▪Use more than one operator (=)
▪Example :
int a = b= c = d = e = 250;
int b =2, number =0, total = 0,average =3;
number = b++ = 10;
int age = workHour = 0;
19
Arithmetic Expression
Compound Assignment Statement
Function
▪To combine two different operator together.
▪To simplify arithmetic operator
▪Original function of operator does not affected
▪Allowed combination are as follow:
operator:
+= , -= , *= , /= , %=
20
Arithmetic Expression
Compound Assignment Statement
Example :
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
21
Arithmetic Operator Precedence Rules
Arithmetic Expression
Compiler will follows the following precedence to execute the arithmetic expression
based on priority.
Operator Arrangement/Priority
() Left to right
++, -- Right to left
*, /, % Left to right
+, - Left to right
22
Arithmetic Operator
Arithmetic Operator Precedence Rules
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
23
Arithmetic Expression
Arithmetic Operator Precedence Rules
Example:
3. Prefix unary arithmetic expression
int kira = 5, nilai_pertama = 10;
nilai_kedua = 5 * --kira + nilai_pertama;
printf(“%d %d”, kira, nilai_kedua);
Output:
4 30
24
Arithmetic Expression
Arithmetic Operator Precedence Rules
Example:
3. Prefix unary arithmetic expression
int kira = 5, nilai pertama = 10;
nilai_kedua = 5 * kira-- + nilai_pertama;
printf(“%d %d”, kira, nilai_kedua);
Output:
4 35
25
Arithmetic Expression
Mathematic Library Function
•Header file designed for basic mathematical operations (math.h)
•Syntax #include <math.h>
•Below are lists of some common math functions :
Fungsi Tujuan
sqrt(x) compute the non-negative square root of x
pow(x,y) computes x raised to the power 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)
26
Arithmetic Expression
Mathematic Library Function
Example:
#include<stdio.h>
#include <math.h>
Output :
void main() 4
{
int x = 16, y ;
y = sqrt(x);
printf(“%d”,y);
27
Arithmetic Expression
Exercise:
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)
28
Arithmetic Expression
Exercise:
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++; 3 d) k = ++j; 5
b) k = i * j--; 20 e) k = i * --j; (5*4)=20
c) k = j + i * j++; (3+5*3)=18 f) k = 27 / j++ - 16 % i; 27/4-16%5
=6.8-1 = 5.8
29
❑Arithmetic operation
❑Relational operation
❑Logical Expression
Relational Expression
Relational use
Relational operator
expression
Combination of more than one statement
Can consists of variable vs variable
produce variable vs constant
constant vs constant
0 (if false) 1(if true)
31
Relational Expression
Relational Operator
Operator Description
== Equal to
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
!= Not equal
32
Relational Expression
P/s:
Example 1:
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)
33
Relational Expression
Example 2 :
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)
34
Relational Expression
P/s:
Example 3: Relational operator has less priority than
other operators.
Start evaluating from left to right.
int a=10, b = 3, c = 7;
(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)
35
Relational Expression
An example program which uses relational expression
#include <stdio.h>
void main()
{ int age;
printf(“\nPlease enter your age >>”);
scanf(“%d”,&age);
if (age > 21) Relational expression
printf(“\nYou are qualified to vote”);
36
❑Arithmetic operation
❑Relational operation
❑Logical Expression
Logical Expression
Logical expression Logical Operator
use
Combination of one or more expressions
Can consists of
Relational expr. vs logical expr.
hasilkan Relational expr. vs variable
Relational expr. vs constant
0 (if false) 1(if true)
38
Logical Expression
Logical Operator
Operator Description
&& AND
|| OR
! NOT
Logical operator && dan || is used between 2 or more
relational expression
39
Logical Expression
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
40
Logical Expression
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
41
Logical Expression
Logical operator truth table for NOT
NOT(!)
Value Result Logical Operator Result
!0 1 Not false True
!1 0 Not true False
42
Logical Expression
Example 1:
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
43
Logical Expression
Example 2:
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
44
Logical Expression
An example program which using Logical Expression:
#include <stdio.h>
void main()
{ int mark;
printf(“\nEnter your mark >>”);
scanf(“%d”,&mark);(800)
if (mark >= 85 && mark <= 100)
printf(“\nGred A”);
else if( mark >=80 && mark <=84)
printf(“\nGred A-”);
}
45
Logical Expression
Exercise:
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);
46
Lab/Class exercise 3 : Submit during Lab session
Lab/Class exercise 3 : Submit during Lab session
Lab/Class exercise 3 : Submit during Lab session