You are on page 1of 42

Chapter 4b

Expression and Operator

BIC 10204 : ALGORITHM AND PROGRAMMING


Objectives

Understand concepts and Write expression in C


fundamentals in programming language
expression/operator.
Introduction

Arithmetic Expression
Sub topics
Relation Expression

Logical Expression
Introduction to Expression

• Given the following statements:

Expression

2x+3-z=y
Process involves in money withdrawal scenario

Expression in C?

Introduction to Expression
Introduction to Expression
Combination of more than one variable
or constant (operand) which separated
by operator
Operand

Example : x+3-z
Operator

Consists of:

Arithmetic Relational Legal


Arithmetic Expression Mathematic
known as Expression

using
Arithmetic
Operator

Unary Binary
Operator Operator
Unary Operator

Operates for one operand

Example : a=-20; b=+15

Computer memory cells: a -20 b +15


Prefix vs Postfix
• Increment: It is used to increment the value of the variable by 1.
The increment can be done in two ways:
• prefix increment: In this method, the operator precedes the operand (e.g.,
++a). The value of the operand will be altered before it is used.

• postfix increment: In this method, the operator follows the operand (e.g.,
a++). The value operand will be altered after it is used.
Prefix vs Postfix
• Decrement: It is used to decrement the value of the variable by 1.
The decrement can be done in two ways:
• prefix decrement: In this method, the operator precedes the operand
(e.g., – -a). The value of the operand will be altered before it is used.

• postfix decrement: In this method, the operator follows the operand (e.g.,
a- -). The value of the operand will be altered after it is used.
Unary Operator Example 1
Unary Operator Example 2
Unary Operator Example 3
Binary Operator

Located between constants or variables or both


combination
Operator
Operands

Example : A + Z

Operator Symbol Mathematic Expression Arithmetic Expression


Multiplication * 2x+y 2*x+y
Divide / 2÷y 2/y
Modulus % 2÷y = 1. Return a balance when 2 numbers is divided
2. Only can be used with an integer variable
Binary Operator Example 1
Assignment Statement Used to store
value/result of
process to a variable

Use operator
symbol =

Double Compound
assignment assignment
statement statement
Assignment Statement
Compound Assignment Statement
Function

Operator

Compound Assignment Statement


Example

Compound Assignment Statement


Compiler will follows the following
precedence to execute the
arithmetic expression based on priority.

Arithmetic Operator Precedence Rules


Example
2. 3 * 4 / 2 + ( 3 – 1 )

3*4/2+ 2

12 /2+ 2

6 + 2

Arithmetic Operator Precedence Rules


Example

Arithmetic Operator Precedence Rules


Example

Arithmetic Operator Precedence Rules


Mathematic Library Function

Function Purpose
sqrt(x) Compute the non-negative square
root of x
pow(x,y) Compute x raised to the power y
cos(x) Compute the cosine of x (measured
in radians)
sin(x) Compute the sine of x (measured in
radians)
tan(x) Compute the tangent of x(measured
in radians)
Mathematic Library Function

Example:

#include <stdio.h>
#include <math.h> Output?

Void main()
{
int x = 16,
y;
y=
sqrt(x);
Exercise
Exercise
Relational Expression Relational
use
operator
Consists of
1. variable vs variable
2. variable vs constant Combination of
3. constant vs constant more than one
statement
Produce

0 (if false) 1 (if true)


Relational
Operator
Relational Expression Example 1

int a = 6, b = 1, c = -2
1. a + b == c

2. a != b Answer:
0 or 1?
3. b < c

4. b + c <= a
Relational Expression Example 2

int a = 10, b = 3, c = 7
(a+b >= 3*c == (a != 2*c+b)

10+3 >= 3*7 == (10 != 2*7+3) Relational operator has less


priority than other operators.
Start evaluating from left to
13 >= 21 == (10 != 17) right.

0 == 1

0 (false)
Relational
Expression –
example
program
Logical Expression
use Logical operator

Consists of

1. Relational exp vs Combination of


logical exp one or more
2. Relational expr vs expression
variable
Produce
3. Relational expr vs
constant
0 (if false) 1 (if true)
Logical operator &&
dan || is used between
2 or more
relational expression
Logical Operator
Logical Operator truth table for AND (&&)
Logical Operator truth table for OR (||)
Logical Operator truth table for NOT (!)
Logical Expression

Example 1

a) (2 < 5) && (5 < 10) b) (7 % 2 < 2) || ( 2 * 3 == 6)


1 && 1 1 || 1

1 1
Logical Expression

Example 2
Given a = 3, b = 4;

a) !((5 * b <= 23 – a)) b) !((b + 3 != 8) && (3 * a < 2))

!(20 <= 20) !(( 7 != 8) && (9 < 2))

!(1) !( 1 ) && (0)

0 !(0)

1
Logical
Expression –
example
program
Exercise

You might also like