FP101 PROGRAMMING PRINCIPLES
CHAPTER 3 FUNDAMENTALS OF PROGRAMMING LANGUAGE
Norzimah Che Hassan 1
Course Learning Outcome (CLO):
Upon completion of this course, students should be able to: 1) Explain the basic computer and programming fundamentals with appropriate examples of language and technology. 2) Apply the different types of algorithm to solve problem efficiently. 3) Solve problem effectively by applying related theories of the basic programming language to a given particular scenario using programming life cycle.
Norzimah Che Hassan
Topic Specification
3.1 Understand data and identifier 3.2 Solve problem using operators in a program 3.3 Apply program control structure
Norzimah Che Hassan
3.1 Understand Data & Identifier
Norzimah Che Hassan
Understand Data & Identifier
What is data? Input for the program Pre-processing fact Data types: Numeric Non-numeric
Norzimah Che Hassan
Data Types: Numeric
A type of data that use to do a calculation only. 2 types:
Integer Number (..-2,-1,0,1,2..) Explicit Number (Floating number)
Norzimah Che Hassan
Data Types: Non Numeric
Consist character, number, words, and specific symbol. We put them inside the apostrophe ( ).
Example: A, task2 , & , ()
Norzimah Che Hassan
Data Types Table :
Data Types Numeric Integer Description Integers only Example -5, -90, 5, 0, 34
Floating Integers + floating numbers
-8.9 , 9.6, 2 9.001, 0.7 A, a, +, ()
Non-numeric
Characters, numbers and symbol
Norzimah Che Hassan
EXERCISE Determine the data types:
2337 0.56 4.7 2 % /
Norzimah Che Hassan
Data Structures/ Hierarchy :
Data Hierarchy refers to the systematic organization of data, often in a hierarchical form. Data organization involves fields, records, files and database.
Bit Byte Field Record File Database
Norzimah Che Hassan 10
BIT
Basic unit of memory. Either 0 or 1 can be stored. Smallest data of unit storage
Norzimah Che Hassan
11
BYTE
Ordered collection of 8 bits Series of bits that represent characters.
Norzimah Che Hassan
12
FIELD
Holds a single fact Consider a date field, e.g. "September 19, 2004 have 3 fields
Month Day of month Year
Column in database
Norzimah Che Hassan
13
Example of Field
MONTH
DAY
YEAR
Norzimah Che Hassan
14
Example of Field
Norzimah Che Hassan
15
RECORD
Collection of related fields Combination field about a thing, person, place etc. Row in database
Norzimah Che Hassan
16
EXAMPLE OF RECORD
Norzimah Che Hassan
17
FILE
Collection of related records Table in database
Norzimah Che Hassan
18
EXAMPLE OF FILE
Norzimah Che Hassan
19
DATABASE
One or more data files / table A database is a collection of data that is organized so that it can easily be accessed, managed, and updated.
Norzimah Che Hassan
20
EXAMPLE OF DATABASE
Norzimah Che Hassan
21
Data Structures
DATA SYSTEM (one or more Database) DATABASE ( one or more Files)
FILE or TABLE (one or more Record)
RECORD or ROW (one or more Fields) FIELD or COLUMN (one or more Bytes) BYTE or CHARACTER (8 bits) BIT
Norzimah Che Hassan 22
Identify Terms:
Identifier Variables Constant
Norzimah Che Hassan
23
IDENTIFIER
The given name to multiple elements in programming such as constant, variables and function.
Norzimah Che Hassan
24
VARIABLES
Location memory Will keep data value The value are changeable during entire programming execution.
Norzimah Che Hassan
25
VARIABLES
Declaration of variables
a = 5; b = 2; a = a + 1; result = a - b;
int a; int b; int c;
Norzimah Che Hassan
26
VARIABLES
// operating with variables #include <iostream> using namespace std; int main () { // declaring variables: int a, b; int result; // process: a = 5; b = 2; a = a + 1; result = a - b; // print out the result: cout << result; // terminate the program: return 0; }
Norzimah Che Hassan 27
CONSTANT
Constants are expressions with a fixed value. The value are not changeable during entire programming execution. Example:
const int days_in_year = 365; const float ChickenPerKg= 17.5;
Norzimah Che Hassan
28
CONSTANT vs. VARIABLES
Constant
Value :- 25
Variables
Variables name: Age Value :- 25, 30, 15 Variables name: Cash Value :- 2.5, 3.0, 1.5
Value : - 3.2
Value :- Kangar
Variables name: City Value :- Ipoh, Kuala Lumpur, Georgetown
Norzimah Che Hassan
29
How to write identifier?
Combination of: Character (A - Z), (a - z) Digit (0 - 9) Underscore (_) Cannot start with digit Not have reserve words No blank space No limit of character usage but the system will identify first 32 character Case sensitive
Norzimah Che Hassan 30
EXERCISE Determine either this is a valid identifier:
1. 2. 3. 4. 5. 6. 7. 8. my_school 100students Vari,ables PoliteknikBalikPulau PLAYINGFOOTBALL Playing Football Saya_suka_membaca %fail
Norzimah Che Hassan 31
3.2 Solve problem Using Operators in a Program
Norzimah Che Hassan
32
Operators in a Program
What is OPERATOR? A symbol to represent particular computer operation. Consists of: i. Arithmetic Operators ii. Relational Operators iii. Logical Operators
Norzimah Che Hassan
33
ARITHMETIC OPERATOR
Norzimah Che Hassan
34
Arithmetic Operator
It have 5 basic operator in programming language: Symbol Operator
+ Addition
_
* / %
Subtraction
Multiplication Division Modulus
Norzimah Che Hassan
35
Arithmetic Operator
Modulus (%)?
To get balance from two handling division. i.e.: a) 5 % 3 is 2
1 3
5 3
2 BALANCE IS 2!
Norzimah Che Hassan 36
b) 17 % 4 is 1.
4
4
17
16 1 BALANCE IS 1!
Norzimah Che Hassan
37
Operator Priority
It have a basic priority that should know :
Operator () * / + % Priority Highest Higher Lower
Norzimah Che Hassan
38
Operator Priority
a) x = 5 + 2 * 4 1 x = 5+2*41 x = 5+81 x = 13 - 1 x = 12
for the same priority, start from left side.
Norzimah Che Hassan
39
Operator Priority
b) x = ( 5 + 2 ) * ( 4 - 1)
x = ( 5 + 2 ) * ( 4 - 1) x = 7 * 3 x = 21
Bracket has a highest priority!!
Norzimah Che Hassan
40
EXERCISE Get the answer for this question: a) 9 + 1 2 b) 2 * 5 + 2 / 1 c) 12 % 5 d) 4 + (7 - 3) * 3 e) ( 2 * 4 ) * 2 ( 6 + 2 ) / 8
Norzimah Che Hassan
41
RELATIONAL OPERATOR
Norzimah Che Hassan
42
Relational Operation
To compare 2 operator. Same data type, i.e. integer, character or string. The result is either TRUE or FALSE.
Symbol > < >= <= == != Description Greater than Less than Greater or equal than Less or equal than Equal with Not equal with
Norzimah Che Hassan 43
LOGICAL OPERATOR
Norzimah Che Hassan
44
Logical Operator
To test some operation Have 3 symbol:
Symbol && || ! Description AND OR NOT
Norzimah Che Hassan
45
Logical Operator
Base on TRUTH TABLE.
P FALSE FALSE TRUE TRUE Q FALSE TRUE FALSE TRUE P && Q FALSE FALSE FALSE TRUE P || Q FALSE TRUE TRUE TRUE
Norzimah Che Hassan
46
Logical Operator
Example:
Given a=3 and b=5; a) x = (a > 0) && (b > 0) The x value is TRUE because 3 is greater than 0 AND 5 is greater than 0 as well. b) x = ( a < b ) && ( b == 0 ) The x value is FALSE . Although 3 is less than 5, but 5 is not equal with zero.
Norzimah Che Hassan 47
EXERCISE
Find x value either TRUE or FALSE from the following equation:
Given a=2 and b=4; a) x = ( a != 0 ) | | b) x = ( a == b ) | | c) x = ! ( a == b ) d) x = ! ( a < b)
( b != 0 ) (b==0)
Norzimah Che Hassan
48
Another Example:
a = ! ( 2 > 5) | | 6 + 3 >= 4 3;
! (FALSE) | | (9 >= 1 ) TRUE | | TRUE
a = TRUE
Norzimah Che Hassan
49
INCREMENT AND DECREMENT OPERATOR
Norzimah Che Hassan
50
Increment And Decrement Operator
Sometimes, we need to increment or decrement a single value in programming. Symbol Operator The value is as below:
++ -Add 1 Minus 1
Valid for variables only i.e. a++, b++, a--, b--, x++, y-- and invalid for constant i.e. 5-- or 9++.
Norzimah Che Hassan 51
Increment And Decrement Operator
The ++ and -- can be put before or after variables. i.e. we can write a++ or ++a, b-- or --b.
Norzimah Che Hassan
52
Example
Given; a=3 b=5 a++ a-b++ b- a value is 4 a value is 2 b value is 6 b value is 4
Norzimah Che Hassan
53
EXERCISE
Given;
x=8
y=6 Find value for: 1. x++ 2. x-3. y++ 4. y 5. ++x 6. --y
Norzimah Che Hassan 54
3.3 Apply Program Control Structures
Norzimah Che Hassan
55
Logical Structures
3 types of program control structures:
i. Sequence ii. Selection iii. Repetition
Norzimah Che Hassan
56
SEQUENCE
Norzimah Che Hassan
57
Sequence
Command set which is execute line by line. Follows logic flow. Example: You need to develop the program which can read student name and count their total mark for one semester. Then the program can print the marks. Formula:
Total marks = continuous evaluation + final evaluation
Norzimah Che Hassan 58
Answer : Algorithm
1. Read student name 2. Read continuous evaluation marks 3. Read final evaluation marks 4. Count total marks by add continuous evaluation and final evaluation 5. Print student name and their total marks.
Norzimah Che Hassan
59
Answer: Pseudo Code
Begin Read student name, continuous evaluation marks, final evaluation marks Count total marks= continuous evaluation + final evaluation Print student name and total marks End
Norzimah Che Hassan
60
Answer : Flow Chart
Start
Read student name, continuous evaluation, final evaluation
Count Total marks = continuous evaluation + final evaluation
Print students name, total marks
End
Norzimah Che Hassan 61
EXERCISE..
Develop a program which can count a room wide.
Wide = Length x Width
Norzimah Che Hassan
62
SELECTION
Norzimah Che Hassan
63
Selection
The expression which result on TRUE or FALSE. Consists 2 keywords:
if else
Norzimah Che Hassan
64
Selection
The selection control structure can be categorized into 4 groups : 1. if statement 2. if-else statement 3. if-else statement (nested) 4. Switch statement
Norzimah Che Hassan
65
IF Statement
The IF statement will perform an action if the condition is true and ignore the action if the condition is false.
Norzimah Che Hassan
66
Example : IF Statement
if speed > 110 print penalty
Norzimah Che Hassan
67
Answer : Algorithm
1. Read speed 2. If speed more than 110, print penalty
Norzimah Che Hassan
68
Answer: Pseudo Code
Begin Read speed if speed> 110, print penalty end if End
Norzimah Che Hassan
69
Answer: Flow Chart
Begin
Read speed
Speed > 110
True
Print Penalty
False End
Norzimah Che Hassan 70
EXERCISE..
If marks < 40 print fail.
Norzimah Che Hassan
71
IF-ELSE Statement
The IF statement will perform an action if the condition is true and perform another action if the condition is false.
Norzimah Che Hassan
72
Example: if-else statement
If speed > 110 print penalty Else print no penalty
Norzimah Che Hassan
73
Answer : Algorithm
1. Read speed 2. If speed more than 110, print penalty 3. Else, print no penalty
Norzimah Che Hassan
74
Answer : Pseudo Code
Begin Read speed if speed> 110, print penalty else print no penalty end if End
Norzimah Che Hassan
75
Answer : Flow Chart
Start
Read speed
Print No Penalty
False
Speed > 110
True
Print Penalty
Norzimah Che Hassan
End
76
EXERCISE..
If marks < 40 print fail Else print pass
Norzimah Che Hassan
77
IF-ELSE Statement (Nested)
The IF-ELSE nested statement is a condition when we have an if in another if body. We use this control structure if we have many selection to be handle with. Syntax:
if .
else if else
Norzimah Che Hassan 78
Example : If-Else Nested Statement
Create a program that can recognize and print the price base on user prompt code. If user enter a non-available value, the program will print Code Not Recognize.
Code 1 2 3 4 Price (RM) 2.00 4.00 6.00 8.00
Norzimah Che Hassan 79
Example: Algorithm
1.Read code 2. If code equal to one, then print RM2.00 3. Else if code equal to 2, then print RM4.00 4. Else if code equal to 3, then print RM6.00 5. Else if code equal to 4, then print RM8.00 6. Else print Code Not Recognize
Norzimah Che Hassan
80
Example: Pseudo Code
Begin Read code if code = 1 print RM2.00 else if code = 2 print RM4.00 else if code = 3 print RM6.00 else if code = 4 print RM8.00 else Print Code Not Recognized End
Norzimah Che Hassan 81
Example: Flow Chart
Start Print Code Not Recognized F
Read code
Code =1
Code =2
Code =3
Code =4
Print RM2.00
Print RM4.00
Print RM6.00
Print RM8.00
End
Norzimah Che Hassan 82
EXERCISE.. Develop a program which can determine a driver expertise by count their training day.
Training day 0 1-3 4 - 10 >10 Expertise None Weak Average Expert
Norzimah Che Hassan
83
REPETITION
Norzimah Che Hassan
84
Repetition
Using loop structure. 3 types of loop:
While Do .. While For
Norzimah Che Hassan
85
While Loop
Repeat as pre-condition is true
while (condition) { loop body }
Norzimah Che Hassan
86
While Loop
The while construct consists of a block of code and a condition. The condition is evaluated, and if the condition is true, the code within the block is executed. This repeats until the condition becomes false. Because while loops check the condition before the block is executed, the control structure is often also known as a pre-test loop.
Norzimah Che Hassan 87
While General
True
Condition
Loop body False
Norzimah Che Hassan
88
While Example
Create a program that can print the value which is less than 5.
Norzimah Che Hassan
89
While Pseudo Code
Begin Read value= 0 while value < 5 Print value value=value+1 end while End
Norzimah Che Hassan 90
While Flow Chart
Start
Value = 0 Value = Value + 1 Value < 5 ? True
Print Value
False
End
Norzimah Che Hassan 91
While Source Code
Norzimah Che Hassan
92
Do-While Loop
Repeat as post-condition is true
do { loop body } while (condition)
Norzimah Che Hassan
93
Do-While Loop
The do while construct consists of a block of code and a condition. First, the code within the block is executed, and then the condition is evaluated. If the condition is true the code within the block is executed again. This repeats until the condition becomes false. Because do while loops check the condition after the block is executed, the control structure is often also known as a post-test loop.
Norzimah Che Hassan 94
Do-While General
True
Loop body
Condition
False
Norzimah Che Hassan
95
Do-While Example
Create a program that can print the value which is less than 5.
Norzimah Che Hassan
96
Do-While Pseudo code
Begin Read value= 0 repeat Print value value=value+1 until value < 5 End
Norzimah Che Hassan 97
Do-While Flow Chart
Start
Value = 0
True Print value Value = Value+1 Value < 5 ? False End
Norzimah Che Hassan
98
Do-While Source Code
Norzimah Che Hassan
99
For loop
Suitable if we know the number of iterations.
for (expr1;expr2;expr3) { s1; s2 ; }
expr1 is executed only once before looping. expr2 is a Boolean expression. If not given, it is assumed to be true. If expr2 is false, the loop is terminated. After execution of the repeat section, expr3 is executed.
Norzimah Che Hassan 100
For loop
The for loop is often distinguished by an explicit loop counter or loop variable. This allows the body of the for loop (the code that is being repeatedly executed) to know about the sequencing of each iteration. for loops are also typically used when the number of iterations is known before entering the loop.
Norzimah Che Hassan
101
For loop
1
2
5
for ( , { 3 printf( } , )
4
);
Norzimah Che Hassan
102
For Example
Create a program that can print the value which is less than 5.
Norzimah Che Hassan
103
For Pseudo code
Begin Read value For value < 5 Print value value=value+1 End for End
Norzimah Che Hassan
104
For Flow chart
Start
Value = 0
Value <5 False End
True Print value value ++
Norzimah Che Hassan
105
For Example
Norzimah Che Hassan
106