Title: CONTROL STRUCTURES: SEQUENTIAL, SELECTIVE, AND REPETITIVE
1CONTROL STRUCTURESSEQUENTIAL, SELECTIVE,AND
REPETITIVE
2- THREE CATEGORIES OF CONTROL STRUCTURES
- SEQUENTIAL
- SELECTIVE
- REPETITIVE
3SEQUENTIAL
CONTROL FLOWS FROM STATEMENT_1 TO STATEMENT_2 AND
SO ON TO STATEMENT_N.
4statement_1
statement_2
. .
statement_n
5SELECTIVE
CONTROL FOLLOWS ONE OF SEVERAL ALTERNATIVES OF
LOGIC FLOW BASED ON A CONDITION.
6ENTER
true
false
logical expression
process
process
EXIT
7REPETITIVE
CONTROL ALLOWS FOR THE REPETITION OF A CERTAIN
TASK BASED ON A CONDITION.
8ENTER
true
process
logical expression
false
EXIT
9EXAMPLE
PROBLEM DESIGN, DEVELOP AND IMPLEMENT A PAYROLL
SYSTEM THAT COMPUTES AN EMPLOYEE'S GROSS PAY. THE
SYSTEM SHOULD ALSO COMPUTE NET PAY USING THE
FOLLOWING CRITERION TO DETERMINE THE AMOUNT TO BE
DEDUCTED FROM THE EMPLOYEE'S GROSS SALARY FOR
SOCIAL SECURITY TAX IF AN EMPLOYEE EARNS MORE
THAN 100.00 IN A WEEK, DEDUCT A TAX OF 25.00
OTHERWISE, DEDUCT NO TAX. PROCESSING SHOULD BE
REPEATED, BASED ON THE USERS REQUEST.
10ANALYSIS AND SPECIFICATIONS 1. DISCUSSION TO
COMPUTE GROSS PAY (THE PROBLEMS OUTPUT), WE MUST
KNOW THE HOURS WORKED AND THE HOURLY RATE (THE
PROBLEMS INPUT). AFTER READING THESE DATA, WE
CAN COMPUTE GROSS PAY BY FINDING THEIR PRODUCT.
NEXT WE CAN COMPUTE THE EMPLOYEE'S NET PAY (THE
PROBLEMS OTHER OUTPUT) BY SUBTRACTING ANY TAX
DEDUCTION FROM THE GROSS PAY.
112. DATA REQUIREMENTS a) INPUT hours (float) //
HOURS WORKED rate (float) // HOURLY RATE final
(int) // NUMBER OF REPETITIONS b) OUTPUT gross
(float) // GROSS PAY net (float) // NET
PAY c) INTERMEDIATE DATA counter (int) //
LOOP CONTROL VARIABLE d) NAMED
CONSTANTS tax_bracket 100.00 // MAXIMUM
SALARY WITHOUT // A TAX DEDUCTION tax
25.00 // AMOUNT OF TAX WITHHELD
123. RELEVANT FORMULAE gross pay hourly rate x
hours worked net pay gross pay - deductions
13DESIGN 1. STRUCTURE CHART
PAYROLL SYSTEM
LEVEL 0
LEVEL 1
COMPUTE GROSS PAY
GET DATA
DISPLAY RESULTS
DISPLAY INSTRUCTIONS
COMPUTE NET PAY
142. MODULE SPECIFICATION (WE INCLUDE ONLY ONE
MODULE DESCRIPTION AS AN EXAMPLE) AS AN
ALTERNATIVE METHOD TO FURTHER AID IN
UNDERSTANDING HOW DATA ARE TRANSMITTED, WE WILL
INCLUDE MODULE SPECIFICATIONS FOR "GROSS PAY"
MODULE. MODULE SPECIFICATIONS FOR THE GROSS PAY
ARE GROSS PAY DATA RECEIVED hours,
rate INFORMATION RETURNED gross LOGIC THE
HOURS ARE MULTIPLIED BY THE PAY RATE TO
PRODUCE THE GROSS PAY.
153. ALGORITHM 1. DISPLAY USER INSTRUCTIONS 2.
WHILE COUNTER lt FINAL 2.1. GET DATA 2.2.
COMPUTE GROSS PAY 2.3. COMPUTE NET PAY 2.3.1. IF
GROSS PAY gt TAXBRACKET THEN DEDUCT
TAX ELSE DEDUCT NO TAX 2.4. DISPLAY RESULTS
16NEW CONCEPTS
LOGICAL EXPRESSIONS, SELECTION, AND REPETITION
17LOGICAL EXPRESSIONS
CONDITIONS ARE WRITTEN IN THE FORM OF LOGICAL
EXPRESSIONS. THERE ARE TWO POSSIBLE VALUES FOR
SUCH AN EXPRESSION 1 (REPRESENTS TRUE), OR 0
(REPRESENTS FALSE).
18- LOGICAL EXPRESSIONS MAY BE FORMED BY USING
COMBINATIONS OF TWO KINDS OF OPERATORS
RELATIONAL AND LOGICAL.
19RELATIONAL OPERATORS
- OPERATOR MEANING
- EQUAL TO
- ! NOT EQUAL TO
- lt LESS THAN
- lt LESS THAN OR EQUAL TO
- gt GREATER THAN
- gt GREATER THAN OR EQUAL TO
20LOGICAL OPERATORS
- OPERATOR MEANING
- AND
- OR
- ! NOT
21- LOGICAL EXPRESSIONS ARE MADE UP OF VARIABLES,
CONSTANTS AND OPERATORS. - operand operator operand
22EXAMPLES
(counter lt FINAL) (class_average lt
70.0) total_grade gt class_average hours_worked
lt 35.0 divisor 0 (angle1 angle2 angle3)
! 180
23 (midterm_grade gt 90) (final_grade gt
90) (side_1 side_2) (side_2 side_3) !
(class_average gt 70.0)
24operand operator operand outcome
1(true) 1(true) 1(true) 1(true)
0(false) 0(false) 0(false) 1(true)
0(false) 0(false) 0(false) 0(false)
25operand operator operand outcome
1(true) 1(true) 1(true) 1(true)
0(false) 1(true) 0(false) 1(true)
1(true) 0(false) 0(false) 0(false)
26operator operand outcome ! 1(true)
0(false) ! 0(false) 1(true)
27- PRECEDENCE OF OPERATORS (REVISITED)
! / - lt lt gt gt !
HIGHEST PRECEDENCE LOWEST PRECEDENCE
EXPRESSION EVALUATION IS LEFT TO RIGHT.
28THE if STATEMENT
- if STATEMENT INSTRUCTIONS ARE EXECUTED IN A
LOGICAL ORDER THAT DIFFERS FROM THEIR PHYSICAL
ORDER. - A CONDITION IS EVALUATED, AND AN ACTION IS
TAKEN DEPENDING ON THE TRUTH VALUE OF THE
CONDITION (THE ANSWER CAN BE EITHER 1 (TRUE) OR 0
(FALSE).
29ONE ALTERNATIVE
if (logical_expression) statement_1
if (total_grade gt class_average) cout ltlt
"passing!" ltlt endl
30LOGIC FLOW OF if WITHONE ALTERNATIVE
ENTER
true
logical expression
false
statement_1
EXIT
31TWO ALTERNATIVES
if (logical expression) statement_1a else
statement_1b
if (total_grade gt class_average) cout ltlt
"passing!" ltlt endl else cout ltlt "in
trouble!!!" ltlt endl
32LOGIC FLOW OF if WITHTWO ALTERNATIVES
ENTER
false
true
logical expression
statement_1a
statement_1b
EXIT
33COMPOUND STATEMENTSAND THE if
THE TASK TO BE CARRIED OUT WITHIN THE if
STATEMENT MAY BE SIMPLE OR COMPOUND. ASSIGNMENT
STATEMENTS, FUNCTION CALLS, INPUT/OUTPUT
STATEMENTS, OTHER if STATEMENTS, ETC. MAY ALSO BE
INCLUDED IN ANY OF THE if ALTERNATIVES.
34EXAMPLE
if (total_grade gt class_average)
passing_grade passing_grade 1 cout ltlt
"passing!" ltlt endl else weak_grade
weak_grade 1 cout ltlt "in trouble!!!" ltlt
endl
35MULTIPLE ALTERNATIVES
if (logical expression_1) statement_1 else if
(logical expression_2) statement_2 . . else
if (logical expression_n) statement_n else
statement_n1
36if (total_grade gt 90) letter_grade
A else if (total_grade gt 80) letter_grade
B else if (total_grade gt 70)
letter_grade C else if (total_grade gt 60)
letter_grade D else letter_grade F
37LOGIC FLOW OF if WITHMULTIPLE ALTERNATIVES
ENTER
false
logical expression_1
true
true
logical expression_2
false
statement_2a
statement_2b
statement_1
EXIT
38LOGIC FLOW OF if WITHMULTIPLE ALTERNATIVES
ENTER
false
true
logical expression_1
true
logical expression_2
false
statement_1a
statement_1b
statement_2
EXIT
39SEQUENTIAL if VERSUSNESTED if STATEMENTS
if (number gt 0) positive_num positive_num
1 if (number lt 0) negative_num negative_num
1 if (number 0) zero_num zero_num 1
40if (total_grade gt 90) letter_grade
A else if (total_grade gt 80) letter_grade
B else if (total_grade gt 70)
letter_grade C else if (total_grade gt 60)
letter_grade D else letter_grade F
41COMPOUND if STATEMENTS
. . if (side_1 side_2) (side_2 side_3)
cout ltlt "Triangle is Equilateral" ltlt endl else
if (side_1 side_2) (side_2 side_3)
(side_1 side_3) cout ltlt "Triangle is
Isosceles" ltlt endl else cout ltlt "Triangle is
Scalene" ltlt endl . .
42THE while STATEMENT
- THE REPETITION OF TASKS IN A PROGRAM IS CALLED
LOOPING. - WITH THE while LOOP, IF A CONDITION EXISTS, TASK
IS EXECUTED. AFTER THE EXECUTION OF THE TASK,
THE CONDITION IS TESTED AGAIN. THIS CONTINUES AS
LONG AS THE CONDITION REMAINS TRUE.
43while (logical_expression) statement
total 0 counter 1 // the following is a
loop while (counter lt 10) cout ltlt "Enter
grade " cin gtgt grade total total
grade counter counter 1
44- WHAT IF THE LOGICAL EXPRESSION IS FALSE TO BEGIN
WITH? - TASK NEVER EXECUTES, AND FLOW OF CONTROL
CONTINUES WITH THE FIRST STATEMENT FOLLOWING THE
LOOP.
45IMPLEMENTATION
// FILE Payroll.cpp // Computes and prints gross
pay and net pay given an // hourly rate and
number of hours worked. Deducts a tax // of 25
if gross salary exceeds 100 otherwise,
deducts // no tax. include ltiostream.hgt //
needed for cin and cout void main() //
FUNCTIONS USED // Display user instructions
void Instruct_user (const float, // IN
max salary for no deductions const
float) // IN tax amount // Find the gross
salary float Compute_gross (float, //
IN number of hours worked float) //
IN hourly payrate
46 // Find the net salary float Compute_net
(float, // IN gross salary const
float, // IN max salary for no deductions
const float) // IN tax amount // LOCAL
DATA const float tax_bracket 100.00 // max
salary for // no deductions const
float tax 25.00 // tax amount float
hours // input hours worked float
rate // input hourly payrate int
final // input number of repetitions float
gross // output gross pay float net //
output net pay int counter //
intermediate loop control variable
47 // Display user instructions Instruct_user
(tax_bracket, tax) // Enter number of
repetitions cout ltlt "Number of repetitions
cin gtgt final counter 1 while
(counter lt final) // Enter hours and
rate cout ltlt "Hours worked " cin gtgt
hours cout ltlt "Hourly rate " cin gtgt
rate // Compute gross salary gross
Compute_gross (hours, rate)
48 // Compute net salary net Compute_net
(gross, tax_bracket, tax) // Print gross
and net cout ltlt "Gross salary is " ltlt gross
ltlt endl cout ltlt "Net salary is " ltlt net ltlt
endl // Increment the counter
counter // end of the while loop
return // end of main ()
49// Display user instructions void Instruct_user
(const float tax_bracket, // IN maximum
salary // for no deduction
const float tax) // IN tax amount cout ltlt
"This program computes gross and net salary."
ltlt endl cout ltlt "A tax amount of " ltlt tax
ltlt " is deducted for" ltlt endl cout ltlt
"an employee who earns more than " ltlt
tax_bracket ltlt endl ltlt endl cout ltlt
"First, enter how many times do you want to "
ltlt "repeat the calculations." ltlt endl ltlt
endl cout ltlt "Then, enter hours worked and
hourly rate" ltlt endl cout ltlt "on separate
lines after the prompts." ltlt endl cout ltlt
"Press ltRETURNgt after typing each number."
ltlt endl ltlt endl return // end of
Instruct_user
50// Find the gross salary float Compute_gross
(float hours, // IN number of hours worked
float rate) // IN hourly payrate //
Compute gross pay return hours rate // end
of Compute_gross // Find the net salary float
Compute_net (float gross, // IN gross
salary const float tax_bracket, // IN max
salary // for no deductions
const float tax) // IN tax amount //
Compute net salary if (gross gt tax_bracket)
return gross - tax // deduct a tax amount
else return gross // deduct no tax
// end of Compute_net