7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 1/472
1
CHAPTER 8:
PROGRAMMING
8.1 Introduction to Programming
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 2/472
2
8.1 Introduction to Programming
8.1.1 Definition8.1.2 Types of Programming Languages
8.1.3 Programming Language Paradigm8.1.4 Translator
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 3/472
3
LEARNING OUTCOME:
At the end of this topic, students shouldbe able to:
1. Define programming language.2. Differentiate between Low-level and
High-level Language.
3. State paradigms of programming language.4. Identify the functions of translators.
8.1 Introduction to Programming
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 4/472
4
What is Programming ???
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 5/472
5
A programming is a series of instructions thatdirects a computer to perform tasks.
– created by a programmer using a
programming language.- instructions must be written in a way thecomputer can understand.
Programming
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 6/472
6
Computer Program - Example
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 7/472
7
What is a ProgrammingLanguage ???
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 8/472
8
Programming Language
Set of words, abbreviations, and symbols
that enables a programmer to communicate
instructions to a computer.
- Discovering Computer 2011 -
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 9/472
9
Programming Language
Languages that are used for…
Writing games
Writing application programs (like Excel)
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 10/472
10
10
8.1.2 Types of Programming Language
Low-levelLanguage
High-levelLanguage
Two Types
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 11/472
11
(1) Low-level Language
Programming language that is machinedependent that runs on only oneparticular type of computer.
These programs are not easily portableto other types of computer.
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 12/472
12
12
MachineLanguage
AssemblyLanguage
Two Types
Low-level Language
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 13/472
13
Low-level Language – Machine
Language
The FIRST generation of programminglanguage
The ONLY language that computer directly recognizes. Machine language instructions use aseries of binary digits
(1s and 0s) or combination of numbersand letters that represent binary digits.
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 14/472
14
Low-level Language – Machine
Language
Disadvantages:
their instructions are extremelycryptic and difficult to learn programs are lengthy Machine-dependent not portable.
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 15/472
15
Low-level Language – Assembly
Language
The SECOND generation of programminglanguage
Using symbolic instruction codes (meaningful abbreviat ions)
- A → addition- C → compare
- L → load- M → multiply
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 16/472
16
Low-level Language – Assembly
Language
Disadvantages:
Difficult to learn. (Programs must be convertto machine language before the computer can execute, or run the program.)Programmers must use assembler to convert
assembly language to machine language.
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 17/472
17
Low-level Language – Assembly
Language
Disadvantages:programs are lengthy.Machine-dependentnot portable.
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 18/472
18
Low-level Language – Assembly
Language
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 19/472
19
(2) High-level Language
• instructions are quite English-like
• easier to learn than machine or assembly language
• a single instruction corresponds to
many operations at the machinelevel
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 20/472
20
High-level Language
have to be converted to machine-languagebefore they can be executed
Portable
examples : FORTRAN, COBOL, BASIC,
Pascal, Ada, C and C++
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 21/472
21
• Computer languages are categorized accordingto the approach they use to solve a problem.
• A paradigm, is a way in whichcomputer language looks at the problem to be
solved. (style/architecture of programming
language)
21
8.1.3 Programming LanguageParadigm
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 22/472
22
FORTRAN
COBOL
BASIC
C
Pascal
Ada
Smaltalk
C++
Visual Basic
Java
C#
F#
LISP
Scheme
Prolog
22
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 23/472
23
Object-Oriented - Java
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 24/472
24
Translator Program -- the program thattranslates the source program into the objectprogram.
Source Program -- program written in a high-level programming language.
Object Program -- the source program after it
has been translated into machine language.
8.1.4 Translator
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 25/472
25
Assembler Interpreter Compiler
convertsprograms writtenin assembly
language tomachine code
translates onehigh-levelprogram
instruction at atime into machinecode
convert all of thesource programinto object
program
25
8.1.4 Translator
Three Types
sanggagakputih.blogspot.com
8 1 Introduction to Programming
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 26/472
26
Definition of programming languageDifferences between low-level and high-
level languageDifferences of the language paradigm inprogrammingFunctions of translator
26
SUMMARY
8.1 Introduction to Programming
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 27/472
27
CHAPTER 8:
PROGRAMMING
8.2 Approach in Problem Solving
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 28/472
28
8.2 Approach in Problem Solving
8.2.1 Input Process Output (IPO) Analysis8.2.2 Algorithm8.2.3 Pseudocode
8.2.4 Flow Chart8.2.5 Control Structure
8.2.5.1 Sequence8.2.5.2 Selection
8.2.5.3 Looping
sanggagakputih.blogspot.com
8 2 Approach in Problem Solving
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 29/472
29
LEARNING OUTCOME:
At the end of this topic, students shouldbe able to:
1. Identify input, process and output from a give
problem.
2. Define algorithm.
3. Solve problem using pseudocode.
4. Solve problem using flow chart.5. a) explain the purpose of each control structure.
b) apply appropriate control structure in problem
solving.
8.2 Approach in Problem Solving
sanggagakputih.blogspot.com
P bl S l i
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 30/472
30
Problem Solving – Everyday in Li fe
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 31/472
31
Problem Solving in Programming
Programming is a process of problem
solving
Problem solving techniques
Analyze the problem
Outline the problem requirements
Specify what the solution should doDesign steps, called an algorithm, to
solve the problem (the general
solution)sanggagakputih.blogspot.com
8 2 1 Input Process Output (IPO) Analysis
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 32/472
32
8.2.1 Input Process Output (IPO) Analysis
• The IPO is used to analyze problems anddevelop algorithms
• Used to organize and summarize theresults of a problem analysis
• It shows where in the solution theprocessing takes place
sanggagakputih.blogspot.com
8 2 1 Input Process Output (IPO) Analysis
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 33/472
33
Input Processing Output
Processing items:
Algorithm:
• It can also be represent using IPO chart
8.2.1 Input Process Output (IPO) Analysis
sanggagakputih.blogspot.com
8 2 1 Input Process Output (IPO) Analysis
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 34/472
34
To do the IPO Analysis, start with:
1- Output (Display the results)
2- Input (Read the data)3- Process (Perform the computation)
Input :
Process :
Output :
Identify
8.2.1 Input Process Output (IPO) Analysis
sanggagakputih.blogspot.com
8 2 1 Input Process Output (IPO) Analysis
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 35/472
35
Output should answer the following question:
What does the user want to see pr in ted
on the pr inter, displayed on the sc reen, or
sto red in a f i le?
8.2.1 Input Process Output (IPO) Analysis
sanggagakputih.blogspot.com
8 2 1 Input Process Output (IPO) Analysis
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 36/472
36
Input should answer the following question:
What info rmat ion w i l l the compu ter need
to know to pr int , disp lay, or s tore the outpu t i tems?
8.2.1 Input Process Output (IPO) Analysis
sanggagakputih.blogspot.com
8 2 1 Input Process Output (IPO) Analysis
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 37/472
37
Processing item:
An in termediate value that the algo r i thm uses
when processing the inpu t into the output
8.2.1 Input Process Output (IPO) Analysis
sanggagakputih.blogspot.com
Analyze the Problem
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 38/472
38
Analyze the Problem
• What is it you are trying to accomplish?
• What outcome you are trying to arrive at?
• List the Inputs and Outputs
• Often you work backwards from the
Output
• List the Outputs, and then figure out what
Inputs you need in order to arrive at the Outputsanggagakputih.blogspot.com
Analyze the Problem
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 39/472
39
Examp le 1 Problem statement:
Calcu late the area o f a rectang le
We can summarize the information contained
in the problem statement as follows
width
height
Analyze the Problem
sanggagakputih.blogspot.com
Analyze the Problem
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 40/472
40
Prob lem Analys is :
To do the IPO Analysis, start with:
1 – Output
2 – Input
3 – Process
Analyze the Problem
Input :
Process :
Output :
Identify
sanggagakputih.blogspot.com
Analyze the Problem
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 41/472
41
Problem Analys is :
Input: width, height
Process: area = width x height
Output : area of rectangle
Analyze the Problem
sanggagakputih.blogspot.com
IPO Chart
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 42/472
42
The Problem Analysis, can also be represent
using IPO (Input, Processing, Output) Chart
O C a t
Input Processing Output
width,height
Processing items:
area = width xheight
Algorithm:
area of rectangle
sanggagakputih.blogspot.com
Analyze the Problem
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 43/472
43
Example 2
Problem statement:
Find the average of three numbers input by
user.
We can summarize the information contained inthe problem statement as follows...
y
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 44/472
Analyze the Problem
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 45/472
45
Problem Analys is :
Input : number1, number2, number3
Process:
average = (number1 + number2 +
number3) / 3
Output : average
y
sanggagakputih.blogspot.com
IPO Chart
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 46/472
46
Input Processing Output
number1, number2,number3
Processing items:
average = (number1 +number2 + number3) /
3
Algorithm:
average
sanggagakputih.blogspot.com
Analyze the Problem
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 47/472
47
Example 3
Problem statement:
Determ ine the total cost o f dur ians, given
the number of k i los o f dur ians purchased
and the cost of dur ians per k i lo .
y
sanggagakputih.blogspot.com
Analyze the Problem
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 48/472
48
Prob lem Analys is :
To do the IPO Analysis, start with:
1 – Output
2 – Input
3 – Process
Input :
Process :
Output :
Identify
sanggagakputih.blogspot.com
Analyze the Problem
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 49/472
49
Problem Analys is :
Input : Number of kilos of durians,
Cost of durians per kilo
Process: Total cost = Number of kilos of durians ×
Cost of durians per kilo
Output :
Total cost of durians
sanggagakputih.blogspot.com
IPO Chart
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 50/472
50
Input Processing Output
Number of kilos of durians,
Cost of durians per kilo
Processing items:
Total cost = Number of kilos of durians x
Cost of durians per kilo
Algorithm:
Total cost of durians
sanggagakputih.blogspot.com
Analyze the Problem
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 51/472
51
Example 4 Problem statement:
JKL Corpo rat ion gives a bonus to Ir fan based
on his sales. The bonus is 5 percent o f the sales. Calcu late Irfan's bonus.
sanggagakputih.blogspot.com
Analyze the Problem
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 52/472
52
Prob lem Analys is :
To do the IPO Analysis, start with:
1 – Output
2 – Input
3 – Process
Input :
Process :
Output :
Identify
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 53/472
53
Problem Analys is :
Input : Irfan's sales,
Bonus rate of 5%
Process: Bonus = Sales x Bonusrate 5%
Output : Irfan's bonus
sanggagakputih.blogspot.com
IPO Chart
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 54/472
54
Input Processing Output
Irfan's sales,Bonus rate of
5%
Processing items:
Bonus = Sales xBonus rate 5%
Algorithm:
Irfan's bonus
sanggagakputih.blogspot.com
8.2.2 Algorithm
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 55/472
55
• The algorithm is the abstract idea of
solving a problem.
• An algo r i thm is a step-by -step
inst ruct ions that will transform input into
output.
• It can be represent using pseudo code or
flow chart.sanggagakputih.blogspot.com
From Algorithms to Programs
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 56/472
56
Problem
C++ Program
Problem Analysis
Algorithm
sanggagakputih.blogspot.com
Algorithm in everyday‟s life
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 57/472
57
g y y
How to make a mug of hot coffee ??
sanggagakputih.blogspot.com
Algorithm in everyday‟s life
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 58/472
58
g y y
How to make a mug of hot coffee:1. Start
2. Boil water
3.Prepare a mug4. Put a tea spoon of coffee & sugar
5. Pour hot water
6. Stir
7. End
sanggagakputih.blogspot.com
8.2.2 Algorithm
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 59/472
59
can be described using three control structures
(Bohm and Jacopini-1966);
•A Sequence – is a series of statements that execu te one after
another
•A Selection – (branch) – statement is used to determine which of two
different statements to execute depending on
cer tain co ndi t ions
•A Looping – (repetition) – statement is used to repeat s tatements whi le certain
condi t ions are met
sanggagakputih.blogspot.com
Planning the Algorithm
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 60/472
60
2. Transfer to pseudo code or f low char t
3. a. Must start with a start
b. Must close with an end
1. Do Problem Analysis
Input :
Process :Output :
Identify
sanggagakputih.blogspot.com
TECHNIQUES TO REPRESENTTHE ALGORITHM
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 61/472
61
THE ALGORITHM
Pseudo code Flow chart
• Artificial, informal
language used todevelop algorithms
• Similar to everyday
English
• Graphical representation
of an algorithm
• Special-purpose
symbols connected byarrows (flow lines)
1.Pseudo code
2.Flow Chart
sanggagakputih.blogspot.com
TECHNIQUES TO REPRESENTTHE ALGORITHM
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 62/472
62
Calculate the area of a circle
Pseudo code Flow chart
Start:Input radiusCalculate circle areacircle area = 3.142 x radius
x radius
Print the circle areaEnd:
ample of Pseudocode & Flow char t
radius
circle area = 3.142 x radius x radius
circle area
start
end
THE ALGORITHM
sanggagakputih.blogspot.com
SUMMARY
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 63/472
63
SUMMARY
1. Identify input, process and output from a givenProblem.
2. Define algorithm.
sanggagakputih.blogspot.com
CHAPTER 8:
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 64/472
64
CHAPTER 8:
PROGRAMMING
8.2 Approach in Problem Solving
sanggagakputih.blogspot.com
LEARNING OUTCOME:
8.2 Approach in Problem Solving
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 65/472
65
LEARNING OUTCOME:
At the end of this topic, students shouldbe able to:
1. Identify input, process and output from a givenproblem.
2. Define algorithm.
3. Solve problem using pseudo code.
4. Solve problem using flow chart.
sanggagakputih.blogspot.com
8.2.2 Algorithm
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 66/472
66
• The algorithm is the abstract idea of
solving a problem.
• An algo r i thm is a step-by -step
inst ruct ions that will transform input into
output.
• It can be represent using pseudo code or
flow chart.sanggagakputih.blogspot.com
From Algorithms to Programs
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 67/472
67
Problem
C++ Program
1) Problem Analysis
2) Algorithm
sanggagakputih.blogspot.com
Algorithm in everyday‟s life
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 68/472
68
How to make a mug of hot coffee ??
sanggagakputih.blogspot.com
Algorithm in everyday‟s life
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 69/472
69
How to make a mug of hot coffee:1. Start
2. Boil water
3. Prepare a mug
4. Put a tea spoon of coffee & sugar
5. Pour hot water
6. Stir
7. End
sanggagakputih.blogspot.com
8.2.2 Algorithm
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 70/472
70
can be described using three control structures
(Bohm and Jacopini-1966);
•A Sequence – is a series of statements that execu te one after
another
•A Selection – (branch) – statement is used to determine which of two
different statements to execute depending on
cer tain co ndi t ions
•A Looping – (repetition) – statement is used to repeat s tatements whi le certain
condi t ions are met
sanggagakputih.blogspot.com
Planning the Algorithm
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 71/472
71
2. Transfer to pseudo code or f low char t
3. a. Must start with a start
b. Must close with an end
1. Do Problem Analysis
Input :
Process :Output :
Identify
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 72/472
TECHNIQUES TO REPRESENTTHE ALGORITHM
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 73/472
73
Calculate the area of a circle
Pseudo code Flow chart
Start:1. Input radius2. Calculate circle area:
circle area = 3.142 xradius x radius
3. Print the circle areaEnd:
ample of Pseudocode & Flow char t
radius
circle area = 3.142 x radius x radius
circle area
start
endsanggagakputih.blogspot.com
8.2.3 Pseudo Code
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 74/472
74
• Artificial, informal language used to develop
algorithms.
•Similar to everyday English.
sanggagakputih.blogspot.com
8.2.3 Pseudo Code
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 75/472
75
Pseudocode Format
start
statement 1statement 2
end
1
3
2
The statement
refers to any
i nput , ou tpu t &
process involved
sanggagakputih.blogspot.com
Example 1
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 76/472
76
Problem statement:
Calcu late the area of a rectangle
width
height
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 77/472
77
Problem Analys is :
Input: width, height
Process: area = width x height
Output : area of rectangle
sanggagakputih.blogspot.com
Pseudo Code – Examp le 1
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 78/472
78
start
1. Input width, height
2. Compute area:area = width x height
3. Print area of rectangle
end
sanggagakputih.blogspot.com
P bl t t t
Example 2
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 79/472
79
Problem statement:
Find the average of th ree numbers input
by user.
sanggagakputih.blogspot.com
Example 2
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 80/472
80
Problem Analys is :
Input : number1, number2, number3
Process: average = (number1 + number2 +
number3) / 3
Output : average
sanggagakputih.blogspot.com
Pseudo Code – Examp le 2
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 81/472
81
start
1. Input number1, number2, number3
2. Compute average:
average = (number1 + number2+ number3) /3
3. Print average
end
sanggagakputih.blogspot.com
P bl t t t
Example 3
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 82/472
82
Problem s tatement:
Determ ine the total cos t of du rians , given
the number of ki los o f dur ians purchased
and the cost of dur ians per k i lo .
sanggagakputih.blogspot.com
P bl A l i
Example 3
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 83/472
83
Problem Analys is :
Input : Number of kilos of durians,
Cost of durians per kilo
Process:
Total cost = Number of kilos of durians ×
Cost of durians per kilo
Output :
Total cost of durians
sanggagakputih.blogspot.com
Pseudo Code – Examp le 3
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 84/472
84
start 1. Input number of kilos of durians,
cost of durian per kilo.
2. Compute total cost of durian:
total cost = number of kilos of durians x cost of
durian per kilo
3. Print total cost of durianend
sanggagakputih.blogspot.com
Example 4
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 85/472
85
Problem statement:
JKL Corpo rat ion gives a bonus to Ir fan based
on his sales. The bonus is 5 percent o f the
sales. Calcu late Ir fan's bonus .
sanggagakputih.blogspot.com
Example 4
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 86/472
86
Problem Analys is :
Input : Irfan's sales,
Bonus rate of 5%
Process: Bonus = Sales x Bonus
rate 5%
Output : Irfan's bonus
sanggagakputih.blogspot.com
Pseudo Code – Examp le 4
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 87/472
87
start
1. Input sales, bonus rate
2. Compute bonus:bonus = sales x bonus rate
3. Print bonus
end
sanggagakputih.blogspot.com
8.2.4 Flow Chart
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 88/472
88
• Graphical
representation of
an algorithm.
• Special-purpose
symbols connected
by arrows (flow
lines).
sanggagakputih.blogspot.com
Graphic Name Meaning
Flow Chart Symbols
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 89/472
Symbol
Terminal Symbol
(ellipse)
indicates the beginning and end
points of an algorithm
Process Symbol
(rectangle)
shows an instruction other than
input, output or selection
Input-Output
Symbol
(parallelogram)
shows an input or output operation
89
89sanggagakputih.blogspot.com
G hi S b l N M i
Flow Chart Symbols
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 90/472
Graphic Symbol Name Meaning
Disk Storage Input-
Output Symbol
(cylinder)
indicates input from or output to disk
storage
Printer Output
Symbolshows hardcopy printer output
Selection Symbol(diamond)
shows a selection process for two-wayselection
90
90sanggagakputih.blogspot.com
Graphic Name Meaning
Flow Chart Symbols
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 91/472
Graphic
Symbol
Name Meaning
Flow Lines
(arrow)
indicates the logical sequence of
execution steps in the algorithm
Off-Page
Connector
provides continuation of a logical path
on another page
On-Page
Connector
(circle)
provides continuation of a logical path at
another point in the same page
91
91sanggagakputih.blogspot.com
Flow Chart - Format
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 92/472
start
Statement 1
Statement 2
end
The
statement
refers to
any input , outpu t &
process
involved
92
92sanggagakputih.blogspot.com
P bl t t t
Example 1
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 93/472
93
Problem statement:
Calcu late the area of a rectangle
width
height
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 94/472
94
Problem Analys is :
Input: width, height
Process: area = width x height
Output : area of rectangle
sanggagakputih.blogspot.com
start
Flow chart - Example 1
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 95/472
area = width x height
end
width, height
area
95
95sanggagakputih.blogspot.com
Problem statement:
Example 2
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 96/472
96
Find the average of th ree numbers input
by user.
sanggagakputih.blogspot.com
Problem Analys is :
Example 2
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 97/472
97
Problem Analys is :
Input : number1, number2, number3
Process: average = (number1 + number2 +
number3) / 3
Output : average
sanggagakputih.blogspot.com
start
Flow chart - Examp le 2
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 98/472
average = (number1 + number2 + number3) / 3
end
number1, number2, number3
average
98
98sanggagakputih.blogspot.com
Problem s tatement:
Example 3
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 99/472
99
Determ ine the total cos t of du rians , given
the number of ki los o f dur ians purchased
and the cost of dur ians per k i lo .
sanggagakputih.blogspot.com
Problem Analys is :
Example 3
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 100/472
100
y
Input : Number of kilos of durians,
Cost of durians per kilo
Process:
Total cost = Number of kilos of durians ×
Cost of durians per kilo
Output :
Total cost of durians
sanggagakputih.blogspot.com
start
Flow chart - Example 3
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 101/472
total cost = number of kilos of durians x costof durian per kilo
end
number of kilos of durians,
cost of durian per kilo
total cost
101
101sanggagakputih.blogspot.com
Problem statement:
Example 4
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 102/472
102
Problem statement:
JKL Corpo rat ion gives a bonus to Ir fan based
on his sales. The bonus is 5 percent o f the
sales. Calcu late Ir fan's bonus .
sanggagakputih.blogspot.com
Problem Analys is :
Example 4
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 103/472
103
Problem Analys is :
Input : Sales,
Bonus rate 5%
Process: Bonus = Sales x Bonus
rate 5%
Output : Bonus
sanggagakputih.blogspot.com
start
Flow chart - Example 4
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 104/472
Bonus = Sales x Bonus rate 5%
end
Sales, Bonus rate 5%
Bonus
104
104sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 105/472
105
EXERCISE
sanggagakputih.blogspot.com
Problem statement:
Exerc ise 1
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 106/472
106
Problem statement:
Pustaka Mukmin Bookstore offers 15%
discount for each revision book. Calculate and
display the price that customer should paid.
Prepare IPO analysis, pseudo code & flow
chart
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 107/472
Problem Analys is :
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 108/472
108
Problem Analys is :
Input : Price
Process: New Price = Price x 85%
Output : New Price
sanggagakputih.blogspot.com
Pseudo Code
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 109/472
109
start 1. Price
2. Compute Discount:
Discount = Price x 15%
3. Compute New Price:New Price = Price – Discount
4. Print New Price
end
sanggagakputih.blogspot.com
start
Flow chart
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 110/472
Discount = Price x 15%New Price = Price - Discount
end
Price
New Price
110
110sanggagakputih.blogspot.com
SUMMARY1. Identify input, process and output from a given
problem.
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 111/472
111
2. Define algorithm.3. Solve problem using pseudo code.4. Solve problem using flow chart.
sanggagakputih.blogspot.com
CHAPTER 8:
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 112/472
112
PROGRAMMING
8.2 Approach in Problem Solving
8.2.5 Control Structure
sanggagakputih.blogspot.com
LEARNING OUTCOME:
8.2.5 Control Structure
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 113/472
113
At the end of this topic, students shouldbe able to:
1. Explain the purpose of each control structure.
2. Apply appropriate control structure in
Problem solving.
sanggagakputih.blogspot.com
8.2.5 Control Structure
Three (3) control structures
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 114/472
114
A Sequence
is a series of statements that execute one after ano ther
A Selection – (branch)
statement is used to determine whichof two different statements to execute
depend ing on certain cond i tions
sanggagakputih.blogspot.com
8.2.5.1 Sequence
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 115/472
115
Control Structures
SELECTION LOOPINGSEQUENCE
sanggagakputih.blogspot.com
What do you do after wake up in the
8.2.5.1 Sequence(Analogy)
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 116/472
116
morning?
Brush teethWash faceTake a bathComb hair Smile in front of mirror Having breakfastGo to class
sanggagakputih.blogspot.com
8.2.5.1 Sequence
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 117/472
117
• The simplest programs consist justsequence of statements :
– no loops, no selections amongstalternative actions.
sanggagakputih.blogspot.com
8.2.5.1 Sequence
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 118/472
118
• Instruction in sequence programming are executed sequentially one by one.
• The sequence structure directs the
computer to process the instructions,one after another, in the order listed in
the program.
first instruction → last instruction. sanggagakputih.blogspot.com
Problem statement:
Example 1
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 119/472
119
Find the produc t of two numbers.
sanggagakputih.blogspot.com
IPO Analys is:
Example 1
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 120/472
120
Input : num1, num2
Process: product = num1 x num2
Output : product
sanggagakputih.blogspot.com
Example 1 – Pseudo Code
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 121/472
121
start
1. Input num1, num2
2. Compute product:
product = num1 x num2
3. Print product
end
sanggagakputih.blogspot.com
start
Examp le 1 - Flow chart
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 122/472
product = num1 x num2
end
num1, num2
product
122
122sanggagakputih.blogspot.com
Problem statement:
Example 2
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 123/472
123
Find a volum e for a cone.
Formula :
sanggagakputih.blogspot.com
IPO Analys is:
Example 2
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 124/472
124
Input : radius, height
Process:
volume = (1/3) x 3.142 x radius x
radius x height
Output : volume
sanggagakputih.blogspot.com
Example 2 – Pseudo Code
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 125/472
125
start
1. Input radius, height
2. Compute volume:
volume = (1/3) x 3.142 x radius xradius x height
3. Print volume
end
sanggagakputih.blogspot.com
start
Flow chart - Examp le 2
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 126/472
volume = (1/3) x 3.142 x radius x radius x height
end
radius , height
volume
126
126sanggagakputih.blogspot.com
C t l St t
8.2.5.2 Selection
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 127/472
127
Control Structures
SELECTION LOOPINGSEQUENCE
sanggagakputih.blogspot.com
Analogy
8.2.5.2 Selection
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 128/472
128
You need to choose to make
“Plain Coffee” or “Coffee Milk”
sanggagakputih.blogspot.com
What is Select ion Struc ture?
8.2.5.2 Selection
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 129/472
129
• The selection structure allows instructions
to be executed non-sequentially.
It compares a condition / expression, and
based on the result of the condition, it takes
a certain course of action.sanggagakputih.blogspot.com
8.2.5.2 Selection
Types of selection structures
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 130/472
130
if-else nested if-else switch
Two-way
selectionMulti-way
selectionMulti-way
selection
if
One-way
selection
sanggagakputih.blogspot.com
The if construct has the general
Selection - (1) if
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 131/472
131
• The if construct has the generalform
if (expression)statement
sanggagakputih.blogspot.com
• Expression is evaluated FIRST.
If i l l
Selection - (1) if
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 132/472
132
• If it evaluates to a non-zero value
(TRUE), then statement is executed.
• If it evaluates to zero (FALSE), the
statement is terminated.
• This structure has ONE entry point
and ONE exit point.
sanggagakputih.blogspot.com
Selection - (1) if
Pseudo Code Format:
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 133/472
133
start
1. inpu t
2. if (express ion)
Statement
end
sanggagakputih.blogspot.com
Selection - (1) if
Flow chart Format:
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 134/472
134
expression
statement
True
False
sanggagakputih.blogspot.com
Selection - (1) if [example]
Problem statement:
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 135/472
135
Problem statement:
Print message “a is bigger than b” if
the value for a is bigger than b.
sanggagakputih.blogspot.com
Selection - (1) if
IPO Analysis:
Input: a b
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 136/472
136
Input: a, b
Process:i f
( a > b)print “a is bigger than b”
Output: message “a is bigger than b”
sanggagakputih.blogspot.com
Pseudocode:
Selection - (1) if
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 137/472
137
start
input a , bif (a > b)
print “a is bigger than b”
end
sanggagakputih.blogspot.com
start
Flow chart
Selection - (1) if
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 138/472
138end
a, b
“a is bigger than b”
a > b
true
false
sanggagakputih.blogspot.com
Selection - (1) if [example 2]
Problem statement:
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 139/472
139
Problem statement:
Print message “PASS” if marks for
computer science test is greater than or equal to 60.
sanggagakputih.blogspot.com
Selection - (1) if-
IPO Analysis:
Input: marks
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 140/472
140
Input: marks
Process:i f ( marks >= 60)
print “ PASS ”
Output: message “ PASS ”
sanggagakputih.blogspot.com
Pseudocode:
Selection - (1) if
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 141/472
141
start
input marks
if (marks >= 60)
print “ PASS ”
end
sanggagakputih.blogspot.com
start
Flow chart
Selection - (1) if
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 142/472
142end
marks
“ PASS ”
marks >= 60
true
false
sanggagakputih.blogspot.com
• The if-else construct has thel f
Selection - (2) if-else
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 143/472
143
The if else construct has thegeneral form
if (expression)statement 1
else
statement 2sanggagakputih.blogspot.com
• Expression is evaluated FIRST.
• If it evaluates to a non-zero value
Selection - (2) if-else
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 144/472
144
If it evaluates to a non zero value
(TRUE), statement 1 is executed.
• If it evaluates to zero (FALSE),
statement 2 is executed.
• Either statement 1 is executed or
statement 2 BUT NOT BOTH.
sanggagakputih.blogspot.com
Selection - (2) if-else
Pseudo Code Format:
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 145/472
145
start
1. input
2. if (exp ress ion ) Statement 1
else
Statement 2
end
sanggagakputih.blogspot.com
Selection - (2) if-else
Flow chart Format:
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 146/472
146
expression
statement 1
True
False
statement 2
sanggagakputih.blogspot.com
Selection - (2) if-else
• Example 1:
Print “Excellent!” when user enter marks
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 147/472
147
greater than or equal to 80, else print
“Sorry, try again”.
sanggagakputih.blogspot.com
Selection - (2) if-else
IPO Analysis:
Input: marks
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 148/472
148
p
Process:i f (marks >= 80)
print “Excellent” else
print “Sorry, try again”
Output: “Excellent!” or “Sorry, try again” sanggagakputih.blogspot.com
start
Pseudocode:
Selection - (2) if-else
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 149/472
149
start
input marks
if (marks >= 80)
print “Excellent!”
else
print “Sorry, try again”
end sanggagakputih.blogspot.com
start
Flow chart
Selection - (2) if-else
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 150/472
150end
marks
“Excellent!”
marks >= 80
true “Sorry, try
again”
false
sanggagakputih.blogspot.com
Selection - (2) if-else
• Example 2:
A hi h h l i i i
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 151/472
151
A high school poetry competition is open
only for students above 15 years old.
Display “Eligible” if the students meetthe requirement, else display “Not
eligible” if otherwise.
sanggagakputih.blogspot.com
Selection - (2) if-else
IPO Analysis:
Input: age
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 152/472
152
Process:i f (age > 15)
print “Eligible” else
print “Not eligible”
Output: “Eligible” or “Not eligible” sanggagakputih.blogspot.com
start
Pseudocode:
Selection - (2) if-else
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 153/472
153
start
input age
if (age > 15)
print “Eligible”
else
print “Not eligible”
end sanggagakputih.blogspot.com
start
Flow chart
Selection - (2) if-else
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 154/472
154end
age
“Eligible”
age > 15
true “Not eligible”
false
sanggagakputih.blogspot.com
Selection - (2) if-else
• Example 3:
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 155/472
155
If x is greater than y, display “x is bigger
than y” else display “x is smaller than y”.
sanggagakputih.blogspot.com
Selection - (2) if-else
IPO Analysis:
Input: x, y
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 156/472
156
Process:i f ( x > y)
print “x is bigger than y” else
print “x is smaller than y”
Output: “x is bigger than y” or “x is smaller than y” sanggagakputih.blogspot.com
start
Pseudocode:
Selection - (2) if-else
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 157/472
157
input x, y
if (x > y)
print “x is bigger than y”
else
print “x is smaller than y”
end sanggagakputih.blogspot.com
start
Flow chart
Selection - (2) if-else
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 158/472
158end
x , y
“x is bigger than y”
x > y
true “x is smaller than
y”
false
sanggagakputih.blogspot.com
• The nested if-else structure can
be nested to any depth.
Selection - (3) nested if-else
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 159/472
159
if (expression 1)
statement 1else if (expression 2)
statement 2
else if (expression n)
statement nelse
last statementsanggagakputih.blogspot.com
• In this nested form, expression 1 is
evaluated first. If it evaluates to non-ero (TRUE) statement 1 is
Selection - (3) nested if-else
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 160/472
160
zero (TRUE), statement 1 is
executed and the entire statement is
terminated.• If not, control goes to the second if,
where expression 2 is evaluated. If it
evaluates to non-zero (TRUE),statement 2 is executed.sanggagakputih.blogspot.com
• If not, expression n is evaluated. If it
evaluates to non-zero (TRUE),statement n is executed
Selection - (3) nested if-else
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 161/472
161
statement n is executed.
• Otherwise, last statement will be
executed.
• Only ONE of the statements will be
executed.sanggagakputih.blogspot.com
start 1. input
Pseudo Code Format:
Selection - (3) nested if-else
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 162/472
162
p
2. if (exp ress ion 1)
statement 1
else if (expression 2)statement 2
else if (expression n)
statement n
elselast statement
end sanggagakputih.blogspot.com
Flow chart Format:Selection - (3) nested if-else
expression 1
True
False
statement 1
statement 1
statement 1
statement 1
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 163/472
163
expression 2
expression n
True
TrueFalse
statement 2
statement n
last statement
False
sanggagakputih.blogspot.com
• Example 1:
if student‟s grade is greater than or equal to 80Print “A”
f
Selection - (3) nested if-else
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 164/472
164
else if student‟s grade is greater than or equal
to 60 Print “B”
else if student‟s grade is greater than or equal
to 50 Print “C”
else
Print “Failed”
sanggagakputih.blogspot.com
IPO Analysis:
Input: marksProcess: i f (marks >= 80)
Selection - (3) nested if-else
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 165/472
165
Process: i f (marks 80)
print “A”
else if (marks >= 60)
print “B” else if (marks >= 50)
pr int “C”
else
pr in t “Failed”
Output: “A” or “B” or “C” or “Failed” sanggagakputih.blogspot.com
start 1. input marks
Pseudocode:
Selection - (3) nested if-else
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 166/472
166
2. if (marks >= 80)
print “A”
else if (marks >= 60)
print “B”
else if (marks >= 50)
pr in t “C”
else
pr in t “Failed” end
sanggagakputih.blogspot.com
Flow chart
Selection - (3) nested if-else
k 80
True“A”
marks
start
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 167/472
167
marks >=80
False
marks >=60
marks >=50
True
TrueFalse
False
“A”
“B”
“C”
“Failed”
end
sanggagakputih.blogspot.com
Selection - (3) nested if-else
• Example 2:
if (x > 0)
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 168/472
168
( )
display "x is positive"
else if (x < 0)display "x is negative"
else
display "x is 0"sanggagakputih.blogspot.com
IPO Analysis:
Input: x Process: i f (x > 0)
Selection - (3) nested if-else
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 169/472
169
display "x is positive"
else if (x < 0)
display "x is negative"else
display "x is 0"
Output: “x is positive” or “x is negative” or “x is 0” sanggagakputih.blogspot.com
start
1 input x
Pseudocode:
Selection - (3) nested if-else
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 170/472
170
1. input x
2. if (x > 0)
display "x is positive"
else if (x < 0)
display "x is negative"
else
display "x is 0"
end
sanggagakputih.blogspot.com
Flow chart
Selection - (3) nested if-else
X > 0
True “x is positive”
x
start
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 171/472
171
X > 0
False
X < 0 True
False
x is positive
“x is negative”
“x is 0”
end
sanggagakputih.blogspot.com
• The switch structure is a more
elegant alternative to the complex
nested if-else structure.
Selection - (4) switch
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 172/472
172
• The switch structure lets user chooseONE of several alternatives.
• Also known as mult ip le-choice statement. sanggagakputih.blogspot.com
• Switch structure has the general form:
switch (expression)
{
Selection - (4) switch
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 173/472
173
case expression 1: statement block
break
case expression 2: statement block
break
case expression n: statement block
break
default : statement block
} sanggagakputih.blogspot.com
• expression must evaluate to an integer
or a character constant.
Selection - (4) switch
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 174/472
174
• Each alternative (except the default
alternative) in the switch block is normallyterminated with a break statement.
sanggagakputih.blogspot.com
• The switch statement is executed as
follows:
Selection - (4) switch
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 175/472
175
– The switch expression is evaluated
FIRST. If it evaluates to expression 1,the statement block for that case is
executed and break statement in the
block terminates the entire switch
statement.sanggagakputih.blogspot.com
• If the switch expression evaluates to
expression 2, the statement block for thatcase is executed and break terminates
th it h t t t
Selection - (4) switch
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 176/472
176
the switch statement.
• If the switch expression DOES NOT
EVALUATE to any of the case
expressions, the statement block for the
default case is executed and thenterminates the switch statement.sanggagakputih.blogspot.com
• A break is NOT necessary for the default
case since its execution will terminate theswitch statement.
Selection - (4) switch
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 177/472
177sanggagakputih.blogspot.com
start
1. input
2. sw itch (exp ression )
{
Pseudo Code Format:Selection - (4) switch
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 178/472
178
case expression 1: statement block
break
case expression 2: statement blockbreak
case expression 'n': statement block
break
default : statement block}
end sanggagakputih.blogspot.com
Flow chart Format:Selection - (4) switch
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 179/472
179sanggagakputih.blogspot.com
• Example 1:
Selection - (4) switch
x Print
1 X is 1
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 180/472
180
1 X is 1
2 X is 2
Other value Value of x unknown
sanggagakputih.blogspot.com
IPO Analysis:
Input: x Process: sw i tch (x)
{
Selection - (4) switch
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 181/472
181
{
case 1: Print "x is 1”
breakcase 2: print "x is 2“
break
default: Print "value of x unknown”}
Output: "x is 1" or "x is 2" or "value of x unknown” sanggagakputih.blogspot.com
Pseudocode:
Selection - (4) switch
start 1. Input x 2. switch (x )
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 182/472
182
{case 1: Print "x is 1“
breakcase 2: Print "x is 2"
breakdefault: Print "value of x unknown”
}end
sanggagakputih.blogspot.com
Flow chartSelection - (4) switch
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 183/472
183sanggagakputih.blogspot.com
EXERCISE
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 184/472
184sanggagakputih.blogspot.com
• Exercise 1:
If student's grade is greater than or
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 185/472
185
equal to 50, print “Passed” else print
“Failed”.
Do IPO analysis, pseudo code and flow
chart.sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 186/472
CHAPTER 8:PROGRAMMING
8.3 Introduction to C++
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 187/472
187sanggagakputih.blogspot.com
8.3 Introduction to C++
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 188/472
188
8.3.1 Program
Structure
8.3.2 Using Compiler
sanggagakputih.blogspot.com
LEARNING OUTCOME:
At the end of this topic, students shouldbe able to:
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 189/472
189
8.3.1 Program Structure
Identify components of C++.
8.3.2 Using Compiler
Edit, compile, link and execute program.
sanggagakputih.blogspot.com
8.3.1 Program Structure
Components of C++:
i. Comment
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 190/472
190
ii. Preprocessor directive
iii. Function
iv.Body
v. Return statementsanggagakputih.blogspot.com
8.3.1 Program Structure• A computer program consists a list of
instructions written in a computer language.
//This is my first C++ program //It prints a line of text
#include <iostream>
i. comment
ii preprocessor
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 191/472
191
#include <iostream> using namespace std;
int main()
{cout << "My first C++ program";
cin.get();
return 0;}
ii.preprocessor
directive
iii. function
iv. body
v. returnstatement
sanggagakputih.blogspot.com
8.3.1 Program Structure// rectangleAREA.cpp // Find Area of rectangle
#include <iostream> using namespace std;
int main()
{
i. comment
ii.preprocessor
directive
iii. function
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 192/472
192
{int width,height,area;
cout << “Enter value for width: ”; cin >> width;cout << “Enter value for height: “;
cin >> height;area = width * height;cout << “Area: “ << area;
cin.get();return 0;
}
iv. body
v. return
statementsanggagakputih.blogspot.com
8.3.1 Program Structure
i. Comment
Style to insert a comment in C++.
i. Text begin with two double slash //
normally used for single line comment
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 193/472
193
- normally used for single line comment.
// Th is is my firs t C++ program
// It p r in ts a line o f tex t
sanggagakputih.blogspot.com
i. Comment
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 194/472
194sanggagakputih.blogspot.com
8.3.1 Program Structure
i. Comment
Style to insert a comment in C++.
ii.Text begin with /* and ends with */
( ibl t i i li )
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 195/472
195
(possibly containing many lines)
/* Th is is my firs t C++ program
It pr ints a l ine of text */
sanggagakputih.blogspot.com
i. Comment
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 196/472
196sanggagakputih.blogspot.com
8.3.1 Program Structurei. Comment
• The purpose to insert a comment: – To document a program
– To improve program readability → help
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 197/472
197
To improve program readability helpother people read and understand a
program.
• Remember – comment DO NOT cause thecomputer to perform any action when the
program is run.sanggagakputih.blogspot.com
8.3.1 Program Structure
ii. Preprocessor directive
- Preprocessor directive is a general instructionto the C++ compiler.
Sh i ( #) d b
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 198/472
198
- Sharp sign ( #) :are processed by
preprocessor before program is compiled.- #include <iostream> → tells the
preprocessor to include in the program the
contents of the input/output stream workproperly.sanggagakputih.blogspot.com
8.3.1 Program Structureii. Preprocessor directive
- header file (with the file extension.h) is stored in
the C++ library.
- each header file contains a set of related
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 199/472
199
functions.
sanggagakputih.blogspot.com
8.3.1 Program Structure
iii. Function
int main() – All C++ programs must have the main()
function
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 200/472
200
function.
–
The keyword void before main() indicatesthat main() will not return any value to the
operating system.
– C++ programs normally begin executing
at function main.sanggagakputih.blogspot.com
iii. Function
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 201/472
201sanggagakputih.blogspot.com
8.3.1 Program Structure
iv. Body Function
- All the statements in a C++ function
(main) must appear between a pair of
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 202/472
202
( ) pp p
curly braces { }.- The group of statements enclosed
between the braces is called a function
block.sanggagakputih.blogspot.com
8.3.1 Program Structure
iv. Body Function
- The body of main may consist of:
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 203/472
203
a) variable declaration and reserved worde.g. : int width,height,area;
reserved wordvariables
sanggagakputih.blogspot.com
8.3.1 Program Structureiv. Body Function
- The body of main may consist of:
b) input / output console
input console: use standard input stream object
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 204/472
204
p p j
cin and input operator, >>, to allow user type
in a value (or values).
e.g. : cin >> width;
sanggagakputih.blogspot.com
8.3.1 Program Structure
iv. Body Function
- The body of main may consist of:
b) input / outpu t conso le
output console: use standard output stream
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 205/472
205
p p
object - cout and the output operator, <<, to
output the message.
e.g. : cout << “Area: “ << area;
sanggagakputih.blogspot.com
8.3.1 Program Structure
iv. Body Function
- The body of main may consist of:c) C++ statement
Every statement must end with semicolon
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 206/472
206
- Every statement must end with semicolon
( ; )e.g. :
i. cout << "My first C++ program”;
ii. area = width * height;sanggagakputih.blogspot.com
8.3.1 Program Structure
v. return statement
return 0;
- is included at the end of main function.
0
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 207/472
207
- value 0 indicates that the program has
terminated successfully.
sanggagakputih.blogspot.com
8.3.2 Using Compiler
program.cpp
(C++ source code)
C++
compiler
program.obj
(object code)
Linker
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 208/472
208
C++ Library
program.exe
(executable program)
Fig. 1: Building a C++ Programsanggagakputih.blogspot.com
8.3.2 Using Compiler
• Edit – enter the program statement
• To write a C++ program, you need to enter theprogram statements by using:
– Text editor (e.g. Notepad, Microsoft Word, etc.)
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 209/472
209
– Integrated Development Environment (IDE)(e.g.
Dev C++, Borland C++, Visual C++, etc)
• The complete program statements called
“source code”.
sanggagakputih.blogspot.com
8.3.2 Using Compiler
• Compile – translating C++ into machine code
(also called “object code”) – Compiler determines the syntax error.
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 210/472
210
• Link – Run the linker – combine the machine
code with code from C++ library; after compiles
is successful.
sanggagakputih.blogspot.com
8.3.2 Using Compiler
• Execute Program – an application can be run.
–Finally, the computer, under the control of itsCPU, executes the program one instruction
at a time.
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 211/472
211
–Compiler detects grammatical (syntax) error
but NOT the program-logic error.
sanggagakputih.blogspot.com
8.3.2 Using Compiler
Editor
Compiler Compile
EditSource code
Tool Step Product
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 212/472
212
Linker
Run
Link
Object code
Executable image
Result / Outputsanggagakputih.blogspot.com
SUMMARY1. Identify components of C++.
2. Edit, compile, link and execute program.
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 213/472
213sanggagakputih.blogspot.com
CHAPTER 8:PROGRAMMING
8.4 Data, Operator and Expression
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 214/472
214sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 215/472
215
8.4.4
sanggagakputih.blogspot.com
At the end of the lesson student should
be able to:
Id tif i bl d d d
8.4.1 Variables & Reserved Word
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 216/472
216
Identify variables and reserved word.
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 217/472
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 218/472
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 219/472
gross_income
city_tax
student_age
It 10
• Student age
• Continue
• 17throw
• Principal+Interest
Valid vs. Invalid
Iden t i f iers / User def ined words
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 220/472
220
Item10
count
Number_of_characters
_abc
ABC123Z7
X_1
• Principal+Interest
• 12
• 3X
• %change
• Data-1
• Myfirst.csanggagakputih.blogspot.com
• A variable is an identifier that is usedto hold a value.
Variables
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 221/472
221
• Must be declared before they can beused.
• The general form of a declaration isdataType variable_list;
sanggagakputih.blogspot.com
Dec lar ing variables:
#include <iostream>
using namespace std;
int main ()
{
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 222/472
222
// declaring variables:char name;
int age, home_address;
float weight, height;
return 0;
}
sanggagakputih.blogspot.com
• Keywords that identify languageentities.
• Have special meanings to the
Keyword / Reserved Words
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 223/472
223
Have special meanings to the
compiler.
• Must appear in the correct location
in a program, be typed correctly, and
be used in the right context.
• Must be typed fully in lowercase.
sanggagakputih.blogspot.com
Keyword / Reserved Words
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 224/472
224sanggagakputih.blogspot.com
At the end of the lesson student should
be able to:
Identify various basic data types.
8.4.2 Data Type
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 225/472
225
Differentiate basic data type and their usage.
sanggagakputih.blogspot.com
A set of data values and a set of operations on those
values. Five classes:
character , char
integer , int
Bas ic Data Type and Dec larat ion
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 226/472
226
floating-point, floatdouble floating-point, double
valueless, void
sanggagakputih.blogspot.com
Data Type: (i) char
Used to declare character variables
(hold character value).
8.4.2 Data Type
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 227/472
227
( )
For example:
char choice;
sanggagakputih.blogspot.com
Used to declare numeric program variables of
integer type. (range: -32768 to 32767)
8.4.2 Data Type
Data Type: (ii) int
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 228/472
228
Example:
int counter;
sanggagakputih.blogspot.com
Use to declare a floating-point variable.
(approximately 6 digits of precision)
8.4.2 Data Type
Data Type: (iii) float
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 229/472
229
( pp y g p )
For example:
float gross_salary;sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 230/472
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 231/472
Type Typical Size in Bits Minimal Range
char 8 0 to 255
int 16 or 32 -32 768 to 32 767
float 32 6 digits of precision
A ll Data Types Defined by th e
ANSI/ISO C Standard
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 232/472
232
double 64 12 digits of precision
ANSI – Am erican National Standards Inst i tu te for C prog ramm ing langu age.
ISO – Internat ional Organization for Standardizat ion sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 233/472
Positive or negative whole numbers with
no fractional part. Consists of an optional plus (+) or a
minus ( –) sign and decimal digits.
In teger Cons tants
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 234/472
234sanggagakputih.blogspot.com
Must begin with a non zero decimal digit
Can contain decimal digit values of 0 - 9.
If the sign is missing, the computer assumes
positive.
RULES for Decimal Integer Constants
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 235/472
235
Commas are not allowed in integer constant.
sanggagakputih.blogspot.com
Example :
-15
0
0179
1F8
Decimal Integer Constants
Valid Invalid
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 236/472
236
+250
7550
const int num = -15;
1,756
sanggagakputih.blogspot.com
Character enclosed in single quotation
marks. Examples:
–'F'
Character Constants
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 237/472
237
–'1'
–'*'
char genderFemale = 'F';
sanggagakputih.blogspot.com
A sequence of any number of characters
surrounded by doub le quotat ion m arks . Useful in prompting users for input.
Can contain any printable characters.
Str ing Constants
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 238/472
238sanggagakputih.blogspot.com
const char name[] = “Hello World!”;
Str ing Constants - declarat ion
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 239/472
239sanggagakputih.blogspot.com
Positive or negative decimal numbers
with an integer part, a decimal point,and a fractional part.
Can be represented either in
Float ing-Point Cons tants
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 240/472
240
convent ional or scient i f ic notation.
sanggagakputih.blogspot.com
Consists of six (6) parts:
A sign A decimal integer (optional )
A decimal point
A decimal fraction (optional )
Scient i f ic Notat ion
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 241/472
241
( p )
The letter e or E followed by a signed or
unsigned integer exponent (optional )
Type suffix f, F, l, or L (optional )
sanggagakputih.blogspot.com
Commas are not allowed.
Three types: –float
–double
Scien t i f ic Notat ion
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 242/472
242
–long double
sanggagakputih.blogspot.com
The following are examples of floating-
point literals:
Scien t i f ic Notat ion
Floating Point Value
5.3876e4 53,876
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 243/472
243
4e-11 0.000000000041e+5 100000
7.321E-3 0.007321
0.5e-6 0.0000005
0.45 0.45
sanggagakputih.blogspot.com
The escape character and any character that
follows. E.g:
Code Meaning
\n New line
\t Horizontal tab
\”
Escape Sequence / Backslash
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 244/472
244
\” Double quote
\’ Single quote
\\ Backslash
\? Question marksanggagakputih.blogspot.com
Escape Sequence / Backs lash : e.g
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 245/472
245sanggagakputih.blogspot.com
Escape Sequence / Backslash :
output
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 246/472
246sanggagakputih.blogspot.com
At the end of the lesson student should
be able to:
clarify operations on variables &
operators that can be used in a
8.4.3 Operator
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 247/472
247
program.
Identify precedence of operators
sanggagakputih.blogspot.com
C++ uses a set of built-in operators.
Operators act on operands.
There are several operators:
8.4.3 Operator
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 248/472
248sanggagakputih.blogspot.com
Operator Meaning
-Subtraction, (also unary minus )
+Addition
*Multiplication
/Division
Arithmetic Operators
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 249/472
249
/
%Modulus Division (produce the remainder
of an integer division)
--Decrement
++Incrementsanggagakputih.blogspot.com
Rules for Evaluat ion of Ar i thm et ic
Expressions:
Evaluate all parenthesized subexpressions,
beginning with the innermost subexpressions in
case they are nested.
e g:
8.4.3 Operator
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 250/472
250
e.g:
bill = 60 + ((hours – 24) * 5);
sanggagakputih.blogspot.com
C++ provides two special operators '++' and '-
-' for incrementing and decrementing the value
of a variable by 1.
The increment/decrement operator can be
used with any type of variable BUT it cannot
be used with any constant
Inc rement and Decrement
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 251/472
251
be used with any constant. Increment and decrement operators each have
two forms, pre and post.
sanggagakputih.blogspot.com
Inc rement and Decrement
Increment Decrement
++x or x++ --x or x--
x = x + 1 x = x - 1
x += 1 x = 1
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 252/472
252
x += 1 x -= 1
Pre: ++x Pre: --x
Post: x++ Post: x--
sanggagakputih.blogspot.com
Expression Meaning
x-- decrease 1 after use in expression
--x decrease 1 before use in expression
x++ increase 1 after use in expression
Inc rement and Decrement
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 253/472
253
x++ increase 1 after use in expression
++x increase 1 before use in expression
sanggagakputih.blogspot.com
++x
x = 10;
z = ++x;
Here
x++
x = 10;
z = x++;
Here
Inc rement and Decrement
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 254/472
254
Here,
z is equal to 11
x is equal to 11
Here,
z is equal to 10
x is equal to 11
sanggagakputih.blogspot.com
--x
x = 10;
z = --x;
Here
x--
x = 10;
z = x--;
Here
Inc rement and Decrement
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 255/472
255
Here,
z is equal to 9
x is equal to 9
Here,
z is equal to 10
x is equal to 9
sanggagakputih.blogspot.com
int x,y;
x = 5;
y = x++ - 2;
int x,y;
x = 5;
y = ++x – 2;
Inc rement and Decrement – e.g : 1
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 256/472
256
Answer:
y = 3x = 6
Answer:
y = 4x = 6
sanggagakputih.blogspot.com
int y,z;
y = 8;
z = y-- + 3;
int y,z;
y = 8;
z = --y + 3;
Inc rement and Decrement – e.g : 2
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 257/472
257
Answer:
y = 7z = 11
Answer:
y = 7z = 10
sanggagakputih.blogspot.com
int x,y;
x = 9;
y = x++ + 8;
int x,y;
x = 9;
y = ++x + 8;
Inc rement and Decrement – exercise 1
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 258/472
258
Answer:
y = 3x = 6
Answer:
y = 4x = 6
sanggagakputih.blogspot.com
int x,y;
x = 9;
y = x++ + 8;
int x,y;
x = 9;
y = ++x + 8;
Inc rement and Decrement – exercise 1
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 259/472
259
Answer:
y = 17x = 10
Answer:
y = 18x = 10
sanggagakputih.blogspot.com
int y,z;
y = 3;
z = y-- - 1;
int y,z;
y = 3;
z = --y - 1;
Inc rement and Decrement – exercise 2
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 260/472
260
Answer:
y = 7z = 11
Answer:
y = 7z = 10
sanggagakputih.blogspot.com
int y,z;
y = 3;
z = y-- - 1;
int y,z;
y = 3;
z = --y - 1;
Inc rement and Decrement – exercise 2
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 261/472
261
Answer:
y = 2z = 2
Answer:
y = 2z = 1
sanggagakputih.blogspot.com
int a,b,n;
a = 5;
b = 3;
Inc rement and Decrement – exercise 3
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 262/472
262
n = ++a * ++b;
Answer: n =
a =
sanggagakputih.blogspot.com
int a,b,n;
a = 5;
b = 3;
Inc rement and Decrement – exercise 3
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 263/472
263
n = ++a * ++b;
Answer: n = 24
a = 6
sanggagakputih.blogspot.com
int a,b,n;
a = 7;
b = 1;
Inc rement and Decrement – exercise 4
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 264/472
264
n = a++ * --b;
Answer: n =
a =
sanggagakputih.blogspot.com
int a,b,n;
a = 7;
b = 1;
Inc rement and Decrement – exercise 4
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 265/472
265
n = a++ * --b;
Answer: n = 0
a = 8
sanggagakputih.blogspot.com
EXERCISE Write the output for the program fragment
given.
1)
int z = 7;
cout << z << endl;
cout << --z << endl;
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 266/472
266
;
cout << z++ << endl;
sanggagakputih.blogspot.com
2) int p,q;
p = 9;
q = --p + 5;
cout << --q << “ \t”; cout << --p << endl;
cout << p++ << endl;
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 267/472
267sanggagakputih.blogspot.com
3) int p,q;
p = 7;
q = p++ - 3;
cout << p << “ \t”; cout << ++p << endl;
cout << q++ << “ \t”;
cout << q << endl;
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 268/472
268
q ;
sanggagakputih.blogspot.com
4) int a = 3,b = 2, c;
c = 3 * ((5/a) + (4*(a-b)) % (a+b-2));
cout << ++c;
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 269/472
269sanggagakputih.blogspot.com
ANSWER
7
6
6
1
2
3
12 77
7
8
9
4
5
4
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 270/472
270
7
sanggagakputih.blogspot.com
The relational operators are used to test the
relation between two values.
All relational operators are binary operators
and therefore require two operands.
A relational expression returns zero (0) when
the relation is FALSE and a non-zero (1)
Relat ional Operato r
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 271/472
271
when it is TRUE.
sanggagakputih.blogspot.com
Operator Meaning
> Greater than
>= Greater than or equal
<Less than
L h l
Relat ional Operator
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 272/472
272
<= Less than or equal
== Equal
!= Not equalsanggagakputih.blogspot.com
Relat ional Operato r - examples
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 273/472
273sanggagakputih.blogspot.com
Logical refers to the ways these relationships
can be connected.
Operator Meaning
&& AND
|| OR
Logical Operator
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 274/472
274
||
! NOT
sanggagakputih.blogspot.com
P Q P&&Q P||Q !P
0 0 0 0 1
0 1 0 1 1
1 0 0 1 0
1 1 1 1 0
Logical Operator – Tru th Tab le
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 275/472
275
1 1 1 1 0
sanggagakputih.blogspot.com
Logical & Relat ional Operator – Example
i. ( 5 < 3 ) && (6 <= 6)
Result: 0 (False)
ii.( 1 < 3 ) || (5 != 6) iii. ! (7 >=8)
TF
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 276/472
276Result: 1 (True) Result: 1 (True)
T T F
sanggagakputih.blogspot.com
The assignment operator '=' is used for
assigning a variable to a value. This operator takes the expression on its right-
hand-side and places it into the variable on its
left-hand-side.
For example:
Ass ignment Operator
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 277/472
277
m = 5;
bill = 200;sanggagakputih.blogspot.com
Other Examples:
a) sumProduct = x * y;
b) bonus = 5 * salary;
c) nett_income = gross – deduction;
d) modulus = a % b;
) BMI idth / (h i ht * h i ht)
Ass ignment Operator
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 278/472
278
e) BMI = width / (height * height);
sanggagakputih.blogspot.com
Precedence Level Operator Associativity
1
(highest)
( ) left to right
2 Unary + unary –
++ --
right to left
right to left
3 * / % left to right
4 + - left to right
5 < <= > >= left to right
Precedence o f A ri thm etic and Relat ional
Operators
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 279/472
279
6 == !== left to right
7
(lowest)
= += -= *= /= %= right to left
sanggagakputih.blogspot.com
Assume a = 10, b = 3, c = 7
1) a + b >= 3 * c
Presedence: Examp le 1
12 3
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 280/472
280
13 >= 21
sanggagakputih.blogspot.com
Assume a = 10, b = 3, c = 7
a != 2 * c + b
10 != 17
Presedence: Examp le 2
1 23
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 281/472
281
Output : 1 (true)
sanggagakputih.blogspot.com
int a;
a = 8 + 5 * 1 % 2 * 4
Output : 12
Presedence: Examp le 3
1 2 34
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 282/472
282sanggagakputih.blogspot.com
At the end of the lesson student should
be able to:
Write expression in correct syntax
8.4.4 Expression
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 283/472
283sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 284/472
Here are some examples of expressions:
(i) nett_salary = (basic_pay + hours * rate) – (socso +
premium + loan);
(ii) (b * b – 4 * a * c) > 0
8.4.4 Expression
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 285/472
285
(iii) (gender=='M' || gender=='F') && age >= 21
sanggagakputih.blogspot.com
8.4.4 Expression
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 286/472
286sanggagakputih.blogspot.com
Mixed mode expression happened when
Constants and variables of different data
types are combined together.
Type casting is used to force an expression to
be of a specific data type.
Mixed Mode Expression
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 287/472
287sanggagakputih.blogspot.com
General forms:
(data_type) expression
OR
Type Casting
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 288/472
288
data_type (expression)
sanggagakputih.blogspot.com
float a, b;
float addition, multiplication; float modulus;
addition = a + b;
multiplication = a * b;
modulus = (int) a % (int) b;
Type Casting - examples
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 289/472
289sanggagakputih.blogspot.com
You can add tabs and spaces to expressions
to make them easier to read.
For example, the following two expressions arethe same:
(i) x=10/y*(127/x);
Space and Parentheses
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 290/472
290
(ii) x = 10 / y * (127 / x); sanggagakputih.blogspot.com
Redundant or additional parentheses do not
cause errors or slow down the execution of an
expression.
You should use parentheses to clarify the
exact order of evaluation, both for yourself andfor others.
Space and Parentheses
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 291/472
291sanggagakputih.blogspot.com
Which of the following two expressions is
easier to read?
x = y/3-34*temp+127;
x = (y/3) - (34*temp) + 127;
Space and Parentheses
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 292/472
292sanggagakputih.blogspot.com
SUMMARY
1. Identify variables and reserved word.2. Identify various basic data types.3. Differentiate basic data type and their usage.
4. Clarify operations on variables & operatorsthat can be used in a program.
5. Identify precedence of operators.6. Write expression in correct syntax.
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 293/472
293sanggagakputih.blogspot.com
CHAPTER 8:PROGRAMMING
8.5 Use of Control Structure
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 294/472
294sanggagakputih.blogspot.com
At the end of the lesson student should
be able to:
Write a program segment by using
appropriate control structure.
8.5 Use of Control Structure
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 295/472
295sanggagakputih.blogspot.com
Control structure refers to the order of execution of instructions in a program.
Types of control structure: – Sequence –
Selection – Looping
Control Structure
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 296/472
296sanggagakputih.blogspot.com
From Algorithms to Programs
Problem
- Problem Analysis (IPO)
- Algorithm (Pseudo code
+ Flow chart)
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 297/472
297C++ Programsanggagakputih.blogspot.com
Series of statements that execu te one after ano ther.
(1) Sequence
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 298/472
298sanggagakputih.blogspot.com
(1) Sequence – example 1
Calcu late and
display area of
a c irc le.
IPO Analysis :
I - radiu s
P - area = 3.142 x radius x radius
O - area
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 299/472
299sanggagakputih.blogspot.com
(1) Sequence – example 1
Pseudo code:
Start
Input radius
Calcu late area:
area = 3.142 x radius x radius
Disp lay area
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 300/472
300
Disp lay area
End sanggagakputih.blogspot.com
(1) Sequence – examp le 1
#include <iostream> using namespace std;
int main()
{ float radius, area;
cin >> radius;
area = 3.142 * radius *radius;
Pseudo c ode:
Start
Input radius Calculate area:
area = 3.142 x radius
x radius
Display area End
D
I
P
O
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 301/472
301
cout << area;
cin.get();return 0;
}NOT USER FRIENDLY !!!
sanggagakputih.blogspot.com
Why??
Because there is NO prompt message for user...
Assume that radius entered by user is 8.7
Sample of ou tput :
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 302/472
302sanggagakputih.blogspot.com
(1) Sequence – examp le 1
#include <iostream> using namespace std;
int main(){
float radius, area;
cout << "Enter radius:";
cin >> radius;
D
I
P
O
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 303/472
303
cin >> radius;
area = 3.142 * radius *radius;sanggagakputih.blogspot.com
(1) Sequence – examp le 1
Sample of ou tpu t :
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 304/472
304sanggagakputih.blogspot.com
(1) Sequence – example 2
Calcu late and display to tal for three (3)
qu izzes below :
Quiz1 → 78.5, Quiz2 → 67.5, Quiz3 → 90.2
IPO Analysis :
I - -
P - to tal = Qu iz1 + Qu iz2 + Qu iz3
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 305/472
305
O - total
sanggagakputih.blogspot.com
(1) Sequence – examp le 2
#include <iostream> using namespace std;
int main(){float Quiz1 = 78.5, Quiz2 =
67.5;
float Quiz3 = 90.2, total;
P
D
O
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 306/472
306
total = Quiz1 + Quiz2 +Quiz3; sanggagakputih.blogspot.com
(1) Sequence – examp le 2
Sample of ou tpu t :
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 307/472
307sanggagakputih.blogspot.com
(1) Sequence – example 3
Calcu late and d isp lay p rice of lap top after
15% discoun t.
IPO Analys is:
I - or i pr ice
P - new pr ice = or i pr ice x 0.85
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 308/472
308
O - new pr ice
sanggagakputih.blogspot.com
(1) Sequence – examp le 3
#include <iostream> using namespace std;
int main(){float ori_price, new_price;
cout << “Insert original price: RM “;
i >> i i
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 309/472
309
cin >> ori_price;
new_price = ori_price *0.85;
sanggagakputih.blogspot.com
(1) Sequence – examp le 3
Sample of ou tpu t :
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 310/472
310sanggagakputih.blogspot.com
The following C++ code contains errors.
Correct the error and rewrite the code.
#include (iostream.h)#include (stdlib.h)
int main(){char name[25];
cout >> 'Enter yourname: ';
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 311/472
311
name: ;cin << name
cout << "Your name is "sanggagakputih.blogspot.com
Errors:
#include (iostream.h)#include (stdlib.h)
int main(){char name[25];
cout >> 'Enter yourname: ';
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 312/472
312
name: ;cin << name
cout << "Your name is "sanggagakputih.blogspot.com
Answer:
#include <iostream.h> #include <stdlib.h>
int main(){char name[25];
cout << “Enter yourname: “;
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 313/472
313
name: ; cin >> name;
cout << "Your name is "sanggagakputih.blogspot.com
Sample of output :
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 314/472
314sanggagakputih.blogspot.com
The program may contains error(s). Correct
the error and rewrite the program.
#include <iostream.h> #include <stdlib.h>
void main(){
double dist_km
double dist_mile;
cout << "\n Enter the distance in
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 315/472
315
cout << "\n Enter the distance in\"KM\:";
CIN >> dist_km;sanggagakputih.blogspot.com
Errors:
#include <iostream.h> #include <stdlib.h>
void main(){
double dist_km
double dist_mile;
cout << “\n Enter the
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 316/472
316
cout << “\n Enter thedistance in \”KM \:”;
CIN >> dist_km;sanggagakputih.blogspot.com
Answer:
#include <iostream.h> #include <stdlib.h>
void main(){
double dist_km;
double dist_mile;
cout << “\n Enter the
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 317/472
317
cout << \n Enter thedistance in \”KM \”:”;
cin >> dist_km;sanggagakputih.blogspot.com
Study the C++ program below. Using problem
analysis, determine input, output and process
involve in this program.
#include <iostream.h> #include <stdlib.h>
int main(){
int feet, yards;
cout << “Enter value (yards)”;
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 318/472
318
;
cin >> yards;feet = 3 * yards;
sanggagakputih.blogspot.com
Answer:
IPO Analys is:
I - yards
P - feet = 3 * yards
O - feet
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 319/472
319
O - feet
sanggagakputih.blogspot.com
Exercise:
(1) Wri te a complete C++ prog ram that w i l l calculate salary earned when user enter number of days
worked and rate per day. Your output should
disp lay number of days worked, rate per day and
amount earned.
(2) Wri te a complete C++ prog ram that w i l l calculate
amount paid for an i tem when u ser enter quant i ty
and pr ice of the item . The ou tput m ust d isplay the
quant i ty, pr ice of the i tem and amount paid.
(3) Write a C++ program that wil l calculate and
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 320/472
320
ou tput the average of two f loat ing-po int numbers
entered from the keyboard.sanggagakputih.blogspot.com
Answer:
(1) Wri te a complete C++ prog ram that w i l l calculate salary earned when user enter number of days
worked and rate per day. Your output should
disp lay number of days worked, rate per day and
amount earned.IPO Analys is:
I - days worked, rate
P - salary = days worked x rate
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 321/472
321O - days worked, rate, salary sanggagakputih.blogspot.com
Program:
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 322/472
322sanggagakputih.blogspot.com
Sample of output :
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 323/472
323sanggagakputih.blogspot.com
Answer:
(2) Wri te a complete C++ prog ram that w i l l calculate amount paid for an i tem when user enter quant ity
and pr ice of the item . The ou tput m ust d isplay the
quant i ty, pr ice of the i tem and amount paid
IPO Analys is:
I - quanti ty, p rice
P - amount paid = quant i ty x pr ice
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 324/472
324O - quantit y, pr ice, amoun t paid sanggagakputih.blogspot.com
Program:
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 325/472
325sanggagakputih.blogspot.com
Sample of output :
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 326/472
326sanggagakputih.blogspot.com
Answer:
(3) Write a C++ program that wil l calculate and ou tput the average of two f loat ing-po int numbers
entered from the keyboard
IPO Analys is:
I - num1, num2
P - average = (num1 + num2) / 2
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 327/472
327
O - average sanggagakputih.blogspot.com
Program:
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 328/472
328sanggagakputih.blogspot.com
Sample of output :
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 329/472
329sanggagakputih.blogspot.com
(2) Selection
• The selection structure allows instructions tobe executed non-sequentially.
It compares a condition / expression, andbased on the result of the condition, it takes a
certain course of action.
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 330/472
330sanggagakputih.blogspot.com
Types of selection structures
if-else nested if-else switch
Two-wayselection
Multi-wayselection
Multi-wayselection
if
One-wayselection
(2) Selection
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 331/472
331sanggagakputih.blogspot.com
(2) Selection [if] – example 1
Print “Passed!” when user enter marks greater
than or equal to 60.
IPO Analysis:
Input: marks
Process: i f (marks >= 60)
print “Passed!”
Output: message “Passed!”
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 332/472
332sanggagakputih.blogspot.com
(2) Selection [if] – example 1
#include <iostream> using namespace std;
int main(){
float marks;
cout << "Enter marks: ";cin >> marks;
if ( k > 60)
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 333/472
333if (marks >= 60)
cout << "\nPassed!";sanggagakputih.blogspot.com
(2) Selection [if] – example 1
Sample of ou tput :
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 334/472
334sanggagakputih.blogspot.com
(2) Selection [if] – example 2
Assign car allowance = 200 and housing
allowance = 800 if job code for a worker is
equal to '1'.
IPO Analysis:
Input: job code
Process: i f (job code == '1')
car allowance = 200housing allowance = 800
Output: -
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 335/472
335sanggagakputih.blogspot.com
(2) Selection [if] – example 2
#include <iostream> using namespace std;
int main(){
char job_code;float car_allowance,
housing_allowance;
cout << "Enter your jobcode: ";
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 336/472
336
code: ;
cin >> job_code;sanggagakputih.blogspot.com
(2) Selection [if] – example 2
Sample of ou tput :
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 337/472
337sanggagakputih.blogspot.com
(2) Selection [if-else] – example 1
Print “Excellent!” when user enter marks
greater than or equal to 80, else print “Sorry,
try again”.
IPO Analysis:
Input: marks
Process: i f (marks >= 80)
print “Excellent” else
print “Sorry, try again”
O t t “E ll t!” “S t i ”
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 338/472
338Output: “Excellent!” or “Sorry, try again”
sanggagakputih.blogspot.com
(2) Selection [if-else] – example 1
#include <iostream> using namespace std;
int main(){
float marks;
cout << "Enter marks: ";cin >> marks;
if (marks >= 80)
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 339/472
339if (marks >= 80)
cout << "\nExcellent";else
sanggagakputih.blogspot.com
(2) Selection [if-else] – example 1
Sample of ou tput :
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 340/472
340sanggagakputih.blogspot.com
(2) Selection [if-else] – example 2
Calculate price of car rental. Customer will charged for RM 60 if total hours less or equal to 24. Otherwise, RM
5 will be charged for next every hour. Display the price
of car rental.
IPO Analysis:
Input: hours
Process: i f (hours <= 24)
amount paid = 60
print amount paidelse
amount paid = 60 + ((hours – 24) x 5 )
print amount paid
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 341/472
341
Output: amount paidsanggagakputih.blogspot.com
(2) Selection [if-else] – example 2
#include <iostream> using namespace std;
int main(){
int hours;float amount_paid;
cout << "Enter totalhours: ";
cin >> hours;
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 342/472
342
cin >> hours;
if (hours <= 24)
sanggagakputih.blogspot.com
(2) Selection [if-else] – example 2
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 343/472
343sanggagakputih.blogspot.com
(2) Selection [if-else] – example 2
Sample of ou tput :
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 344/472
344sanggagakputih.blogspot.com
(2) Selection [nested if-else] – example
1
This program prompts the user to enter a
number. If that number is greater than zero, it
tells the user that a positive number was
entered. If the number is less than zero, ittells the user that a negative number was
entered. If both are not true, it tells the user
that the number is equal to zero.
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 345/472
345sanggagakputih.blogspot.com
(2) Selection [nested if-else] – example
1
IPO Analysis:
Input: num
Process: i f (num > 0)
print “Positive Number” else if (num < 0)
print “Negative Number”
else
print “Number is equal to zero”
Output: message “Positive Number” OR
“Negative Number” OR
“Number is equal to zero”
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 346/472
346sanggagakputih.blogspot.com
#include <iostream> using namespace std;
int main(){
int num;
cout << "Enter anynumber: ";
cin >> num;
(2) Selection [nested if-else] – example
1
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 347/472
347
if (num > 0)sanggagakputih.blogspot.com
348
(2) Selection [nested if-else] – example
1
k ih bl
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 348/472
348sanggagakputih.blogspot.com
349
Samples of output :
(2) Selection [nested if-else] – example
1
k tih bl t
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 349/472
349sanggagakputih.blogspot.com
350
(2) Selection [nested if-else] – example
2
Computes and prints the product or total of
three numbers based on the user request. If
user request is 1, the program will count the
product of three numbers. If it is 2, theprogram will count the total of three numbers.
If the number is not 1 or 2, display this
message; “You make a wrong choice”.
sanggagakputih blogspot com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 350/472
350sanggagakputih.blogspot.com
351
(2) Selection [nested if-else] – example
2
IPO Analysis:
Input: user request, num1, num2, num3
Process: i f (user request == 1)
product = num1 x num2 x num3print product
else if (user request == 2)
total = num1 + num2 + num3
print total
else print “You make a wrong choice!”
Output: product OR total
OR message “You make a wrong choice!” sanggagakputih blogspot com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 351/472
351sanggagakputih.blogspot.com
352
(2) Selection [nested if-else] – example
2
sanggagakputih blogspot com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 352/472
352sanggagakputih.blogspot.com
353
Samples of output :
(2) Selection [nested if-else] – example
2
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 353/472
353sanggagakputih.blogspot.com
354
(2) Selection [nested if-else] – example
2
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 354/472
gg g p g p
355
Samples of output :
(2) Selection [nested if-else] – example
2
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 355/472
gg g p g p
356
(2) Selection [switch] – example 1
Computes and prints the product or total of
three numbers based on the user request. If
user request is 1, the program will count the
product of three numbers. If it is 2, theprogram will count the total of three numbers.
If the number is not 1 or 2, display this
message; “You make a wrong choice”.
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 356/472
357
(2) Selection [switch] – example 1
IPO Analysis:
Input: user request, num1, num2, num3
Process: swi tch (user request)
{
case 1 : product = num1 x num2 x num3
print product
break
case 2 : total = num1 + num2 + num3
print total
break
default: print “You make a wrong choice!”
}
Output: product OR totalsanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 357/472
OR message “You make a wrong choice!”
358
(2) Selection [switch] – example 1
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 358/472
359
Switch Nested if-elseequivalent
switch (user_request)
{
case 1 : product = num1 * num2 *
num3;
cout << "\nProduct of threenumber is: " << product;
break;
case 2 : total = num1 + num2 + num3;
cout << "\nTotal of three number
is: " << total;
Break;
default: cout << "\nYou make a wrong
choice!";
`if (user_request == 1)
{
product = num1 * num2 * num3;
cout << "\nProduct of three number is:
" << product;
}
else if (user_request == 2)
{
total = num1 + num2 + num3;
cout << "\nTotal of three number is: "<< total;
}
else
cout << "\nYou make a wrongsanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 359/472
} choice!";
360
(2) Selection [switch] – example 2
This program needs user to key in a number to
make a choice for their favorite's holiday
destination in Malaysia. If the choice is equal
to 1, it will display “Pulau Langkawi”. If thechoice is equal to 2, it will display “Cameron
Highland”. If the choice is equal to 3, it will
display “Genting Highland”. If the choice is
equal to 4, it will display “Bukit Merah”. Other than that, display “Your choice is not in the
list”.sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 360/472
361
(2) Selection [switch] – example 2
IPO Analysis:
Input: user choice
Process: swi tch (user choice)
{
case 1 : print “Pulau Langkawi”
breakcase 2 : print “Cameron Highland”
break
case 3 : print “Genting Highland”
break
case 4 : print “Bukit Merah” break
default: print “Your choice is not in the list”
}
Output: “Pulau Langkawi” or “Cameron Highland” or
“Genting Highland” or “Bukit Merah” or “Yousanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 361/472
g g
make a wrong choice!”
362
(2) Selection [switch] – example 2
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 362/472
363
(2) Selection [switch] – example 2
Samples of output :
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 363/472
364
Switch Nested if-elseequivalent
switch (user_choice)
{
case 1 : cout << "\nPulau Langkawi ";
break;
case 2 : cout << "\nCameron Highland ";break;
case 3 : cout << "\nGenting Highland ";
break;
case 4 : cout << "\nBukit Merah ";break;
default: cout << "\nYour choice is not in
the list";
}
`if (user_choice == 1)
cout << "\nPulau Langkawi ";
else if (user_choice == 2)
cout << "\nCameron Highland ";
else if (user_choice == 3)
cout << "\nGenting Highland ";
else if (user_choice == 4)
cout << "\nBukit Merah ";
else
cout << "\nYour choice is not in the list";sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 364/472
365
What is the output for this program if color value is 'R'
?
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 365/472
366
Output :
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 366/472
367
What is the output for this program code?
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 367/472
368
Output :
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 368/472
369
Study the following fragment code.
int n;
cout << "Enter integer: " <<endl;
cin >> n;
if (n < 5)cout << "\nan integer entered
less than 5 ";
else if (n > 5)cout << "\nan integer entered
greater than 5 ";
(i) What will be the output if user enters the integer value is 0
(ii) What values for n will cause the output “an integer entered not
interesting” ? sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 369/472
else
370
(i) What will be the output if user enters
the integer value is 0
Answer : an in teger entered less than
5
(ii) What values for n will cause the
output “an integer entered not
interesting” ?
Answer : n = 5 sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 370/472
371
(3) Looping
• The looping (or repetition) structure allows
a sequence of instructions to be executed
repeatedly until a certain condition is
reached.
• The looping structure has three forms:
• while• do-while
• for sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 371/472
372
Display “Hello” for five (5) times
IPO Analys is:
I - none
P - none
O - “Hello” for five (5) times
Looping – (1) while – example 1Looping – (1) while – example 1
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 372/472
373
Looping – (1) while – example 1
start
1. ini t ial ize coun ter
i = 1
2. wh ile (i <= 5)
{ pr int “Hello”
3. coun ter increment
i = i + 1
}
4. repeat un ti l i > 5
end
start
end
“Hello”
i <= 5
T
F
i = 1
i = i + 1
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 373/472
374
Looping – (1) while – example 1
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 374/472
375
Looping – (1) while – example 1
Output :
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 375/472
376
Find the average of th ree num bers fo r three
(3) times
IPO Analys is:
I - num1, num2, num3
P - average = (num1 + num2 + num3) / 3
O - average (3 times)
Looping – (1) while – example 2
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 376/472
377
Looping – (1) while – example 2
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 377/472
378
Looping – (1) while – example 2
Sample of outpu t :
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 378/472
379
Calcu late area of a circ le two fo r (2) t imes
IPO Analys is:
I - rad ius
P - area = 3.142 x radius x radius
O - area (2 times)
Looping – (1) while – example 3
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 379/472
380
Looping – (1) while – example 3
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 380/472
381
Looping – (1) while – example 3
Sample of outpu t :
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 381/472
382
Looping – (1) while – t race outpu t 1
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 382/472
383
Looping – (1) while – answer
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 383/472
384
Looping – (1) while – t race outpu t 2
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 384/472
385
Looping – (1) while – answer
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 385/472
386
Display “C++” for seven (7) times
IPO Analys is:
I - none
P - none
O - “C++” (7 times)
Looping – (2) do-while – example 1
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 386/472
387
start
1. ini t ial ize coun ter
i = 1
2. do
{ pr int “C++”
3. coun ter increment
i = i + 1
} 4. wh ile ( i <=7)
repeat wh i le i <= 7
end
start
end
“C++”
i <= 7T
F
i = 1
i = i + 1
Looping – (2) do-while – example 1
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 387/472
388
Looping – (2) do-while – example 1
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 388/472
389
Looping – (2) do-while – example 1
Output :
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 389/472
390
Looping – (2) do-while – example 2
Find the average of th ree num bers fo r three
(3) times
IPO Analys is:
I - num1, num2, num3
P - average = (num1 + num2 + num3) / 3
O - average (3 times) sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 390/472
391
Looping – (2) do-while – example 2
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 391/472
392
Calcu late area of a circ le two fo r (2) t imes
IPO Analys is:
I - rad ius
P - area = 3.142 x radius x radius
O - area (2 times)
Looping – (2) do-while – example 3
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 392/472
393
Looping – (2) do-while – example 3
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 393/472
394
Display “I Love KMM” for ten (10) times
IPO Analys is:
I - none
P - none
O - “I Love KMM” (10 times)
Looping – (3) for – example 1
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 394/472
395
Looping – (3) for – example 1
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 395/472
396
Looping – (3) for – example 1
Output :
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 396/472
397
Looping – (3) for – example 2
Find the average of th ree num bers fo r three
(3) times
IPO Analys is:
I - num1, num2, num3
P - average = (num1 + num2 + num3) / 3
O - average (3 times) sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 397/472
398
Looping – (3) for – example 2
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 398/472
399
Calcu late area of a circ le two fo r (2) t imes
IPO Analys is:
I - rad ius
P - area = 3.142 x radius x radius
O - area (2 times)
Looping – (3) for – example 3
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 399/472
400
Looping – (3) for – example 3
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 400/472
401
EXERCISE (1)
Change the C++ segment code below into for & do-while statement.
int i = 7;
while ( i <= 77)
{ cout << i << endl;
i = i + 7;
}
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 401/472
402
EXERCISE (2)
Change the C++ segment code below intowhile & do-while statement.
int j;
for (j=20; j >= 2; j-=2)
{
cout << j << endl;
}
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 402/472
403
EXERCISE (3)
The following C++ codes containe errors.
Correct the errors and rewrite the code.#include <iostream.h>
#include <stdlib.h>
int main[]
{ int Num, Power, bil;
cout << “Please key in any number: “;
cin >> Num;
for (bil == 1, bil <= 12; bil++;)
{ Power = bil x num;
cout << bil << “ x ” << num << “ = “ << Power << endl;
}
cin.get();
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 403/472
404
EXERCISE (4)
a) Write a program segment that will display
value 1 to 100 in increments of 1.(while, do-while, for)
b) Write a program segment that will displayvalue 100 to 1 in decrements of 1.
(while, do-while, for)
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 404/472
405
EXERCISE (1) - Answer
f or statement.int i;
for (i=7; i <= 77; i=i+7)
{
cout << i << endl;
}
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 405/472
406
EXERCISE (1) - Answer
do-while statement.int i = 7;
do
{ cout << i << endl;
i = i + 7;
} while(i <= 77);
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 406/472
407
EXERCISE (2) - Answer
while statement.
int j = 20;
while (j >= 2)
{ cout << j << endl;
j-=2;
}sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 407/472
408
EXERCISE (2) - Answer
do-while statement.
int j = 20;
do
{ cout << j << endl;
j-=2;
} while (j >= 2);sanggagakputih.blogspot.com
EXERCISE (3)
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 408/472
409
EXERCISE (3) - Answer
#include <iostream.h>
#include <stdlib.h>
int main( )
{ int Num, Power, bil;
cout << “Please key in any number: “;
cin >> Num;
for (bil = 1; bil <= 12; bil++)
{ Power = bil * Num;cout << bil << “ x ” << Num << “ = “ << Power << endl;
}
cin.get();
return 0;}
sanggagakputih.blogspot.com
EXERCISE (4)
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 409/472
410
EXERCISE (4) - Answer
a) Write a program segment that will display
value 1 to 100 in increments of 1.(while, do-while, for)
int j = 1;
while (j <= 100)
{ cout << j << endl;
j++;
} sanggagakputih.blogspot.com
EXERCISE (4) A
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 410/472
411
EXERCISE (4) - Answer
b) Write a program segment that will display
value 100 to 1 in decrements of 1.(while, do-while, for)
int j = 100;
do
{ cout << j << endl;
j--;
} while (j >= 1); sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 411/472
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 412/472
413
Expression for accumulating: (e.g:) sum = sum + num
total = total + marks
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 413/472
414
Example 1:
Calculate and display sum of odd numbers from1 to 10
IPO Analys is:
I - none
P - sum = sum + counter
O - sum sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 414/472
415
int counter = 1;
int sum = 0;
while ( counter <= 10 )
{sum = sum + counter;
counter = counter + 2;
}
cout << “Sum of ODD numbers is: “<< sum;
Example 1: while
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 415/472
416
Example 1: while
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 416/472
417
int counter = 1;
int sum = 0;
do
{
sum = sum + counter;counter = counter + 2;
}
while ( counter <= 10 );
cout << “Sum of ODD numbers is: “<< sum;
Example 1: do-while
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 417/472
418
Example 1: do-while
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 418/472
419
int counter;
int sum = 0;
for (counter = 1; counter <= 10; counter = counter + 2){
sum = sum + counter;
}
cout << “Sum of ODD numbers is: “<< sum;
Example 1: for
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 419/472
420
Example 1: for
sanggagakputih.blogspot.com
E l 1
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 420/472
421
Output :
Example 1:
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 421/472
422
Example 2:
Calculate and display sum of EVEN numbersfrom 2 to 20
IPO Analys is:
I - none
P - sum = sum + counter
O - sum sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 422/472
423
int counter = 2;
int sum = 0;
while ( counter <= 20 )
{sum = sum + counter;
counter = counter + 2;
}
cout << “Sum of EVEN numbers is: “<< sum;
Example 2: while
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 423/472
424
Example 2: while
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 424/472
425
int counter = 2;
int sum = 0;
do
{
sum = sum + counter;counter = counter + 2;
}
while ( counter <= 20 );
cout << “Sum of EVEN numbers is: “<< sum;
Example 2: do-while
sanggagakputih.blogspot.com
E l 2 d hil
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 425/472
426
Example 2: do-while
sanggagakputih.blogspot.com
E l 2 f
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 426/472
427
int counter;
int sum = 0;
for (counter = 2; counter <= 20; counter = counter + 2){
sum = sum + counter;
}
cout << “Sum of EVEN numbers is: “<< sum;
Example 2: for
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 427/472
Example 2:
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 428/472
429
Output :
Example 2:
sanggagakputih.blogspot.com
E ample 3
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 429/472
430
Example 3:
Calculate and display average of three (3)quizzes.
IPO Analys is:
I - mark
P - to tal = to tal + mark
average = total / 3
O - average sanggagakputih.blogspot.com
Example 3: while
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 430/472
431
int counter = 1;
float total = 0, mark, average;
while ( counter <= 3 )
{
cout << “Enter mark: “ ; cin >> mark;
total = total + mark;
counter++;
}average = total / 3;
cout << “Average for 3 quizzes: “<< average;
Example 3: while
sanggagakputih.blogspot.com
Example 3: while
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 431/472
432
Example 3: while
sanggagakputih.blogspot.com
Example 3: do while
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 432/472
433
Example 3: do-while
int counter = 1;
float total = 0, mark, average;
do
{ cout << “Enter mark: “ ;
cin >> mark;total = total + mark;
counter++;
} while ( counter <= 3 );
average = total / 3;cout << “Average for 3 quizzes: “<< average;
sanggagakputih.blogspot.com
Example 3: do while
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 433/472
434
Example 3: do-while
sanggagakputih.blogspot.com
Example 3: for
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 434/472
435
Example 3: for
int counter;
float total = 0, mark, average;
for (counter=1; counter <= 3; counter++)
{ cout << “Enter mark: “ ;
cin >> mark;total = total + mark;
}
average = total / 3;
cout << “Average for 3 quizzes: “<< average;
sanggagakputih.blogspot.com
Example 3: for
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 435/472
436
Example 3: for
sanggagakputih.blogspot.com
Example 3:
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 436/472
437
Sample of Output :
p
sanggagakputih.blogspot.com
Exercise 1
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 437/472
438
Compute the product of all the numbersfrom 1 to 8.
(Hint: Use a variable
called product instead of sum and initialize product to 1)
sanggagakputih.blogspot.com
Exercise 2
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 438/472
439
Find the sum of the square of all thenumbers from 1 to 5.
(i.e. 1*1 + 2*2 + 3*3 +... )
sanggagakputih.blogspot.com
Exercise 1 - Answer
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 439/472
440sanggagakputih.blogspot.com
Exercise 1 - Output
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 440/472
441sanggagakputih.blogspot.com
Exercise 2 - Answer
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 441/472
442sanggagakputih.blogspot.com
Exercise 2 - Output
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 442/472
443sanggagakputih.blogspot.com
8.5 Use of Control Structure
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 443/472
444
Write a program segment by usingappropriate control structure.
SUMMARY
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 444/472
445
CHAPTER 8:
PROGRAMMING
8.6 Header File and Functions
8.6.1 Standard Header file
8.6.2 Functions
sanggagakputih.blogspot.com
8 6 Header file and Functions
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 445/472
446
At the end of the lesson student should
be able to:
1) use standard header file in a program
2) use appropriate functions in a program
8.6 Header file and Functions
sanggagakputih.blogspot.com
Introduction
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 446/472
447
Introduction
Header file is a text file containing smallbits of program code, which is used todescribe the contents of the main body of code to other modules.
Also known as pre-processor directive.
The header file tells the compiler what isavailable in the library.
sanggagakputih.blogspot.com
8.6.1 Standard header file
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 447/472
448
•
The following header files are suppliedwith the standard libraries:
. iostream.h
. stdlib.h OR cstdlib.h
. time.h OR ctime.h
. math.h
http://www.cplusplus.com/reference/clibrary/
sanggagakputih.blogspot.com
i) iostream.h – input outpu t stream
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 448/472
449
)
Declares objects that control reading fromand writing to the standard streams.
The only header files to perform input and output in a C++ program.
#include <iostream.h>
sanggagakputih.blogspot.com
i) iostream.h – input outpu t stream
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 449/472
450
Defines global objects:
)
sanggagakputih.blogspot.com
i) iostream.h – example
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 450/472
451
# include <iostream.h>
int main()
{
cout << “My first C++ program”; cout <<
endl;
cin.get();
return 0;
}
sanggagakputih.blogspot.com
i) iostream.h – example
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 451/472
452
#include <iostream.h>
int main()
{
char name [30];
cout << “Enter Your Name: ”;cout << endl;
cin >> name;
cin.get();
return 0;
}
sanggagakputih.blogspot.com
i) iostream.h – example
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 452/472
453sanggagakputih.blogspot.com
ii) cstdlib.h (stdlib.h)
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 453/472
454
Used for testing and converting characters.
This header defines several general purposefunctions, including dynamic memory
management, random number generation,communication with the environment, integer arithmetic, searching, sorting and converting.
#include <stdlib.h>
sanggagakputih.blogspot.com
ii) cstdlib.h (stdlib.h)
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 454/472
455sanggagakputih.blogspot.com
ii) cstdlib.h (stdlib.h)
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 455/472
456
#include <stdlib.h>
#include <iostream.h>
#include <time.h>
int main( )
{
unsigned masa = time(NULL) ;cout<<“masa = “<< masa <<endl ;
srand(masa); //Initialize random number generator
for (int i=0 ; i<8 ; i++)cout << rand( ) << endl;
return 0;
}
sanggagakputih.blogspot.com
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 456/472
457sanggagakputih.blogspot.com
Sample of outpu t :
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 457/472
458
p p
sanggagakputih.blogspot.com
ii) cstdlib.h (stdlib.h)
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 458/472
459sanggagakputih.blogspot.com
iii) ctime.h (time.h)
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 459/472
460
This header file contains definitions of
functions to get and manipulate date and timeinformation.
Declares the structure tm, which includes atleast the following objects:
int tm_sec seconds [0,61] int tm_min minutes [0,59] int tm_hour hour [0,23] int tm_mday day of month [1,31] int tm_mon month of year [0,11] int tm_year years since 1900sanggagakputih.blogspot.com
iii) ctime.h (time.h)
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 460/472
461sanggagakputih.blogspot.com
iii) ctime.h (time.h)
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 461/472
462
Output:sanggagakputih.blogspot.com
iv) cmath (math.h)
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 462/472
463
cmath declares a set of functions tocompute common mathematical
operations and transformations.
#include <math.h>
sanggagakputih.blogspot.com
iv) cmath (math.h)
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 463/472
464sanggagakputih.blogspot.com
iv) cmath (math.h)
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 464/472
465sanggagakputih.blogspot.com
iv) cmath (math.h) - example
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 465/472
466
Output:sanggagakputih.blogspot.com
8.6.2 Functions
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 466/472
467
A function is a sub-program that programcan call to perform a task.
When you have a piece of code that isrepeated often you should put it into afunction and call that function instead of repeating the code.
sanggagakputih.blogspot.com
8.6.2 Functions -Creating your own function
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 467/472
468
Declare a function in a similar way as createthe main function.
Here is a function that prints Hello.
void PrintHello()
{
cout << "Hello\n";}
sanggagakputih.blogspot.com
8.6.2 Functions -Calling a function
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 468/472
469
Once you have created the function you must call it
from the main program. Here is an example of how to call the PrintHello
function.
void PrintHello()
{
cout << "Hello\n";
}
int main()
{
PrintHello();
return 0;
}
sanggagakputih.blogspot.com
8.6.2 Functions -Calling a function
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 469/472
470sanggagakputih.blogspot.com
Summary
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 470/472
471
1) use standard header file in a program2) use appropriate functions in a program
sanggagakputih.blogspot.com
CHAPTER 8:
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 471/472
472
CHAPTER 8:
PROGRAMMING
8.7 Simple C++ Program
sanggagakputih.blogspot.com
8.7 Simple C++ Program
7/27/2019 Topic 8 Programing
http://slidepdf.com/reader/full/topic-8-programing 472/472
473
At the end of the lesson student shouldbe able to:
1) Write a simple C++ program.
sanggagakputih.blogspot.com