You are on page 1of 21

CHAPTER 5:

Operation Statements

BEE1222: COMPUTER PROGRAMMING

1 NLR- credit to YAW,HAA,RAK & AIH


Syllabus

• Arithmetic Operation
5.1 and C Operators

• Trigonometry
5.2 Functions

2 NLR- credit to YAW,HAA,RAK & AIH


Lesson Outcomes
1. Able to write program to solve expression
statements

3 NLR- credit to YAW,HAA,RAK & AIH


5.1 Arithmetic Operation & C Operators

Main types of C operators:


- Arithmetic
- Logical
- Relational

4 NLR- credit to YAW,HAA,RAK & AIH


Arithmetic
 Performing arithmetic on numeric data
 Arithmetic expression is written as...
 identifier = operand operator operand

C Operation Arithmetic Operator


Addition +
Subtraction -
Multiplication *
Division /
Modulus %
Decrement --
Increment ++

5 NLR- credit to YAW,HAA,RAK & AIH


Arithmetic
Examples of arithmetic Statement Equivalent
x+=3.0 x=x+3.0;

voltage/=sqrt(2); voltage=voltage/sqrt(2);
i-- i=i – 1

i++ i=i + 1
(i is incremented AFTER I has been used)
++i i incremented before i is used

--i i decrease before i is used

6 NLR- credit to YAW,HAA,RAK & AIH


Arithmetic
 Prefix vs Postfix

Operator Example Description Equivalent Statements

++ i++; Postfix i=i+1; i+=1;


++ ++i; Prefix i=i+1; i+=1;
-- i--; Postfix i=i-1; i-=1;
-- --i; Prefix i=i-1; i-=1;

7 NLR- credit to YAW,HAA,RAK & AIH


Example (Prefix increment)

8 NLR- credit to YAW,HAA,RAK & AIH


Example (Postfix increment)

9 NLR- credit to YAW,HAA,RAK & AIH


Exercise 1
Determine all x value in the following output.

Value of x : 4
Value of x + + : 4
New value of x : 5
Value of x - - : 5
?
New value of x : ?
4

Value of + + x : ?
5

New value of x : ?
5
Value of - - x : ?
4
New value of x : ?
4

10 NLR- credit to YAW,HAA,RAK & AIH


Logical
Operator Meaning
! NOT
&& AND
|| OR

Example :

( (5 == 5) && (3 > 6) ) // evaluates to false ( true && false ).


( (5 == 5) || (3 > 6) ) // evaluates to true ( true || false ).

11 NLR- credit to YAW,HAA,RAK & AIH


Example
 This code checks whether an input alphabet is a vowel or
not. Both lower-case and upper-case are checked.

12 NLR- credit to YAW,HAA,RAK & AIH


Relational
 Relational operator = use for data comparison
 The result is TRUE (1) and FALSE (0)

Operator Meaning
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal
!= Not equal

13 NLR- credit to YAW,HAA,RAK & AIH


Order in Expressions

1 + 2 * 3 - 4 / 5 =
1 + (2 * 3) - (4 / 5)

B stands for brackets, B.O.D.M.A.S.


O for Order (exponents),
D for division,
M for multiplication,
A for addition, and
S for subtraction.

14 NLR- credit to YAW,HAA,RAK & AIH


Writing Mathematical formula in C

Mathematical Formula C Expression

b2-4ac b*b-4*a*c

a+b (a+b)/(c+d)
c+d

a=πr2 a=PI*r*r

15 NLR- credit to YAW,HAA,RAK & AIH


5.2 Trigonometry Functions
Math Library Function

 Contains trigonometry  Examples:


functions  abs(int val)
 Pre-Processor header  fabs(double val)
declaration  exp(double val)
 #include <math.h>  log(double val)
 sqrt(double val)
 pow(double val1,
double val2)
 M_PI

16 NLR- credit to YAW,HAA,RAK & AIH


Example
Write a program that able to calculate the value of cosine that
enter by user

17 NLR- credit to YAW,HAA,RAK & AIH


Example
 Pow function
 pow function returns x raise to the power y where x and y are
variables of double data type.

18 NLR- credit to YAW,HAA,RAK & AIH


Exercise 2
 Write a program that able to calculate the value of
square root that enter by user

19 NLR- credit to YAW,HAA,RAK & AIH


Exercise 3
 Write a program that can display output as shown in
figure below. Assume the number is entered by the user.

20 NLR- credit to YAW,HAA,RAK & AIH


END OF CHAPTER 5
1. Q& A
2. Outcomes
 Able to write program to solve expression statements
3. Reflection

21 NLR- credit to YAW,HAA,RAK & AIH

You might also like