0% found this document useful (0 votes)
3 views

Chapter4b-Expression and Operator

Uploaded by

ai240024
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Chapter4b-Expression and Operator

Uploaded by

ai240024
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

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
Sub topics Expression
Relation Expression

Logical Expression
Introduction to Expression

• Given the following statements:

Expressio
n

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


Mathemat
Arithmetic Expression ic
known as Expressio
n

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
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 Arithmetic Expression


Expression
Multiplicatio * 2x+y 2*x+y
n
Divide / 2÷y 2/y
Modulus % 2÷y = 1. Return a balance when 2 numbers is
Binary Operator Example 1
Used to store
Assignment Statement 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> Output?
#include
<math.h>

Void main()
{
int x =
16, y;
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

You might also like