bic 10303 alg
TRANSCRIPT
![Page 1: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/1.jpg)
CHAP 3: STRUCTURE OF PROGRAMMING
![Page 2: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/2.jpg)
Chapter : 3
3. EXPRESSIONS
AND OPERATIONS
Arithmetic operation
Relational operation
Assignment operation
Conditional operation
To give understanding on:
Expression and operator concept
math.h and stdlib.h built-in function
Students should be able to:
understand concepts and fundamentals in
expression/ operator.
write expression in C programming language
Objective
Aim
![Page 3: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/3.jpg)
3
Given the following statement :
2x + 3 – z = y
expression
Introduction to Expression
Example:
Process involves in money withdrawal
scenario
balance – drew out money = current balance
Expression in C :
bakiTerkini =wangKeluar – bakiSemasa;
![Page 4: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/4.jpg)
4 What is Expression?
ExpressionCombination of more than one variable or constant
(operand) which separated by operator
x + 3 - z
Operand
Operatorexample
Consists of arithmetic
relational
logical
Introduction to Expression
![Page 5: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/5.jpg)
5
Arithmetic Expression Mathematic ExpressionKnown as
usingArithmetic Operator
Unary operator Binary operator
Represents by
Operator Meaning
- negative
+ positive
-- decrement
++ increment
Represents by
Operator Meaning* multiply
/ divide
+ add
- subtraction
% modulus
Arithmetic Expression
![Page 6: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/6.jpg)
6 Unary Operator
Unary Operator Operates for one operand
example
a = -20; a -20
Computer memory cell
b = +15; b +15
Increment Decrement
prefix ++c; --c;
postfix c++; c--;
Arithmetic Expression
![Page 7: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/7.jpg)
7 Unary OperatorExample 1:
int A = 5;
++A;
printf(“%d”, A); output ?
A--;
printf(“%d”,A); output ?
A++;
printf(“%d”,A); output ?
Arithmetic Expression
![Page 8: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/8.jpg)
8 Unary Operator
Example 2:
int A ;
A=6;
printf(“%d”,3 + --A); output ?
printf(“%d”, A); output ?
A=6;
printf(“%d”, 3 + A--); output ?
printf(“%d”, A); output ?
Arithmetic Expression
![Page 9: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/9.jpg)
9 Unary Operator
Example 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;
Arithmetic Expression
![Page 10: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/10.jpg)
10 Binary Operator
Binary OperatorLocated between constants or
variables or both combination
example
A + z
operator
operand
Arithmetic Expression
![Page 11: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/11.jpg)
11 Binary Operator
Multiplication Use symbol “ * ”
exampleA * z
operator
operand
Mathematic
2x + y
Arithmetic Expression
2 * x + y
Arithmetic Expression
![Page 12: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/12.jpg)
12
Binary Operator
Divide Use symbol “/”
exampleA / z
operator
operand
Mathematic
2 ÷ y
Arithmetic Expression
2 / y
Arithmetic Expression
![Page 13: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/13.jpg)
13 Binary Operator
Modulus Use symbol “%”
exampleA % z
operator
operand
Return a balance when 2 numbers is divided
Can only be used with an integer variable
Arithmetic Expression
![Page 14: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/14.jpg)
14
Binary OperatorExample:
int A, B;
float C;
A= 2;
B=5;
C= 2.4;
B% A;
C % A;
Valid! Answer is 1
Invalid! C is float
Arithmetic Expression
![Page 15: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/15.jpg)
15
Assignment StatementUsed to store value/result of process to a variable
Use operator symbol =
Assignment statement
Double assignment
statement
Compound assignment
statement
Arithmetic Expression
![Page 16: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/16.jpg)
16
Assignment Statement Format /sintax :
variable = value;
variable = constant; or variable = variable;
variable = expression;
Example :
1.average= ( 6 + 5) * 4;
2.grossSalary = 1500;
nettSalary = grossSalary + 200;
3.price= 50.00;
pay = price;
`
44average
grossSalary1500
nettSalary 1700
price
pay
50.00
50.00
Arithmetic Expression
![Page 17: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/17.jpg)
17
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;
Arithmetic Expression
![Page 18: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/18.jpg)
18 Compound Assignment Statement
To combine two different operator together.
To simplify arithmetic operator
Original function of operator does not affected
Allowed combination are as follow:
Arithmetic Expression
+= , -= , *= , /= , %=
Function
operator:
![Page 19: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/19.jpg)
19 Compound Assignment StatementExample :
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
Arithmetic Expression
![Page 20: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/20.jpg)
20
Arithmetic Operator Precedence Rules
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
Arithmetic Expression
![Page 21: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/21.jpg)
21
Example:
1. 5 + 2 * 6 – 4 / 2
5 + 12 - 4 / 2
5 + 12 - 2
17 - 2
15
2. 3 * 4 / 2 + ( 3 –1)
3 * 4 / 2 + 2
12 / 2 + 2
6 + 2
8
Arithmetic Operator
Arithmetic Operator Precedence Rules
![Page 22: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/22.jpg)
22
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
Arithmetic ExpressionArithmetic Operator Precedence Rules
![Page 23: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/23.jpg)
23
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
Arithmetic Expression
Arithmetic Operator Precedence Rules
![Page 24: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/24.jpg)
24 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)
Arithmetic Expression
![Page 25: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/25.jpg)
25
Mathematic Library FunctionExample:
#include<stdio.h>
#include <math.h>
void main()
{
int x = 16, y ;
y = sqrt(x);
printf(“%d”,y);
}
Output :
4
Arithmetic Expression
![Page 26: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/26.jpg)
26 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)
Arithmetic Expression
![Page 27: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/27.jpg)
27 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++; d) k = ++j;
b) k = i * j--; e) k = i * --j;
c) k = j + i * j++; f) k = 27 / j++ - 16 % i;
Arithmetic Expression
![Page 28: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/28.jpg)
28
Relationalexpression
useRelational operator
Combination of more than one statement
variable vs variable
variable vs constant
constant vs constant
Can consists of
produce
0 (if false) 1(if true)
Relational Expression
![Page 29: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/29.jpg)
29
Relational OperatorOperator Description
= = Equal to
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
!= Not equal
Relational Expression
![Page 30: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/30.jpg)
30
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)
P/s:
a, b and c are variables,
Replace with the given values
Relational Expression
Example 1:
![Page 31: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/31.jpg)
31
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)
Relational Expression
Example 2 :
![Page 32: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/32.jpg)
32
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)
P/s:
Relational operator has less priority than
other operators.
Start evaluating from left to right.
Relational Expression
Example 3:
![Page 33: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/33.jpg)
33An example program which uses relational expression
#include <stdio.h>
void main()
{ int age;
printf(“\nPlease enter your age >>”);
scanf(“%d”,&age);
if (age > 21)
printf(“\nYou are qualified to vote”);
}
Relational expression
Relational Expression
![Page 34: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/34.jpg)
34
Logical expression
use
Logical Operator
Combination of one or more expressions
Relational expr. vs logical expr.
Relational expr. vs variable
Relational expr. vs constant
Can consists of
hasilkan
0 (if false) 1(if true)
Logical Expression
![Page 35: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/35.jpg)
35Logical Operator
Operator Description
&& AND
|| OR
! NOT
Logical operator && dan || is used between 2 or more
relational expression
Logical Expression
![Page 36: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/36.jpg)
36
Logical operator truth table for AND
Value 0 1
0 0 0
1 0 1
AND (&&)Logical Operator Result
False AND False False
False AND True False
True AND False False
True AND True True
Logical Expression
![Page 37: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/37.jpg)
37
Logical operator truth table for OR
Value 0 1
1 0 1
1 1 1
OR (||)Logical Operator Result
False OR False False
False OR True True
True OR False True
True OR True True
Logical Expression
![Page 38: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/38.jpg)
38
Value Result
!0 1
!1 0
NOT(!)
Logical Operator Result
Not false True
Not true False
Logical Expression
Logical operator truth table for NOT
![Page 39: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/39.jpg)
39
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
1
Logical Expression
![Page 40: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/40.jpg)
40Example 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
Logical Expression
![Page 41: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/41.jpg)
41An example program which using Logical Expression:
#include <stdio.h>
void main()
{ int mark;
printf(“\nEnter your mark >>”);
scanf(“%d”,&mark);
if (mark >= 85 && mark <= 100)
printf(“\nGred A”);
else if( mark >=80 && mark <=84)
printf(“\nGred A-”);
}
Logical Expression
![Page 42: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/42.jpg)
42 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);
Logical Expression
![Page 43: BIC 10303 ALG](https://reader030.vdocument.in/reader030/viewer/2022013000/61c97f1f4e443f35f858ad81/html5/thumbnails/43.jpg)
Next Chapter : 4
CONTROL STRUCTURE
4.1 The use of control structure including if,
for and switch
4.2 The use of while loop, break and goto