1 Introduction to Computer Science with Fadi P. Deek CONTROL STRUCTURES: SEQUENTIAL, SELECTIVE, AND...
-
date post
19-Dec-2015 -
Category
Documents
-
view
223 -
download
0
Transcript of 1 Introduction to Computer Science with Fadi P. Deek CONTROL STRUCTURES: SEQUENTIAL, SELECTIVE, AND...
1
Introduction to Computer Sciencewith Fadi P. Deek
CONTROL STRUCTURES:
SEQUENTIAL, SELECTIVE,AND REPETITIVE
2
Introduction to Computer Sciencewith Fadi P. Deek
THREE CATEGORIES OF CONTROL STRUCTURES:
• SEQUENTIAL
• SELECTIVE
• REPETITIVE
3
Introduction to Computer Sciencewith Fadi P. Deek
SEQUENTIAL
CONTROL FLOWS FROM STATEMENT_1 TO STATEMENT_2 AND SO ON TO STATEMENT_N.
5
Introduction to Computer Sciencewith Fadi P. Deek
SELECTIVE
CONTROL FOLLOWS ONE OF SEVERAL ALTERNATIVES OF LOGIC FLOW BASED ON A CONDITION.
6
Introduction to Computer Sciencewith Fadi P. Deek
process process
logical expression
true false
ENTER
EXIT
7
Introduction to Computer Sciencewith Fadi P. Deek
REPETITIVE
CONTROL ALLOWS FOR THE REPETITION OF A CERTAIN TASK BASED ON A CONDITION.
9
Introduction to Computer Sciencewith Fadi P. Deek
EXAMPLE:
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 USER’S REQUEST.
10
Introduction to Computer Sciencewith Fadi P. Deek
ANALYSIS AND SPECIFICATIONS:
1. DISCUSSION
TO COMPUTE GROSS PAY (THE PROBLEM’S OUTPUT), WE MUST KNOW THE HOURS WORKED AND THE HOURLY RATE (THE PROBLEM’S INPUT). AFTER READING THESE DATA, WE CAN COMPUTE GROSS PAY BY FINDING THEIR PRODUCT. NEXT WE CAN COMPUTE THE EMPLOYEE'S NET PAY (THE PROBLEM’S OTHER OUTPUT) BY SUBTRACTING ANY TAX DEDUCTION FROM THE GROSS PAY.
11
Introduction to Computer Sciencewith Fadi P. Deek
2. DATA REQUIREMENTS
a) INPUThours (float) // HOURS WORKEDrate (float) // HOURLY RATEfinal (int) // NUMBER OF REPETITIONS
b) OUTPUTgross (float) // GROSS PAYnet (float) // NET PAY
c) INTERMEDIATE DATAcounter (int) // LOOP CONTROL VARIABLE
d) NAMED CONSTANTStax_bracket = 100.00 // MAXIMUM SALARY WITHOUT
// A TAX DEDUCTIONtax = 25.00 // AMOUNT OF TAX WITHHELD
12
Introduction to Computer Sciencewith Fadi P. Deek
3. RELEVANT FORMULAE
gross pay = hourly rate x hours worked
net pay = gross pay - deductions
13
Introduction to Computer Sciencewith Fadi P. Deek
PAYROLLSYSTEM
COMPUTE GROSS PAY
GET DATA
DISPLAYRESULTS
LEVEL 0
LEVEL 1
DISPLAYINSTRUCTIONS
COMPUTENET PAY
DESIGN:
1. STRUCTURE CHART
14
Introduction to Computer Sciencewith Fadi P. Deek
2. 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 PAYDATA RECEIVED: hours, rateINFORMATION RETURNED: grossLOGIC: THE HOURS ARE MULTIPLIED BY THE PAY
RATE TO PRODUCE THE GROSS PAY.
15
Introduction to Computer Sciencewith Fadi P. Deek
3. ALGORITHM:
1. DISPLAY USER INSTRUCTIONS2. WHILE COUNTER <= FINAL
2.1. GET DATA2.2. COMPUTE GROSS PAY2.3. COMPUTE NET PAY
2.3.1. IF GROSS PAY > TAXBRACKET THEN
DEDUCT TAXELSE
DEDUCT NO TAX2.4. DISPLAY RESULTS
16
Introduction to Computer Sciencewith Fadi P. Deek
NEW CONCEPTS
LOGICAL EXPRESSIONS, SELECTION, AND REPETITION
17
Introduction to Computer Sciencewith Fadi P. Deek
LOGICAL 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
Introduction to Computer Sciencewith Fadi P. Deek
LOGICAL EXPRESSIONS MAY BE FORMED BY USING COMBINATIONS OF TWO KINDS OF OPERATORS:
RELATIONAL AND LOGICAL.
19
Introduction to Computer Sciencewith Fadi P. Deek
RELATIONAL OPERATORS
OPERATOR MEANING
== EQUAL TO
!= NOT EQUAL TO < LESS THAN <= LESS THAN OR EQUAL TO > GREATER THAN >= GREATER THAN OR EQUAL
TO
20
Introduction to Computer Sciencewith Fadi P. Deek
LOGICAL OPERATORS
OPERATOR MEANING
&& AND
|| OR ! NOT
21
Introduction to Computer Sciencewith Fadi P. Deek
LOGICAL EXPRESSIONS ARE MADE UP OF
VARIABLES, CONSTANTS AND OPERATORS.
operand operator operand
22
Introduction to Computer Sciencewith Fadi P. Deek
EXAMPLES:
(counter <= FINAL)
(class_average < 70.0)
total_grade >= class_average
hours_worked <= 35.0
divisor == 0
(angle1 + angle2 + angle3) != 180
23
Introduction to Computer Sciencewith Fadi P. Deek
(midterm_grade >= 90) && (final_grade >= 90)
(side_1 == side_2) || (side_2 == side_3)
! (class_average >= 70.0)
24
Introduction to Computer Sciencewith Fadi P. Deek
operand 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)
&& TRUTH TABLE
25
Introduction to Computer Sciencewith Fadi P. Deek
|| TRUTH TABLE
operand 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)
26
Introduction to Computer Sciencewith Fadi P. Deek
! TRUTH TABLE
operator operand outcome
! 1(true) 0(false)
! 0(false) 1(true)
27
Introduction to Computer Sciencewith Fadi P. Deek
PRECEDENCE OF OPERATORS (REVISITED)
!* / %+ -
< <= >= >== !=&&||=
HIGHEST PRECEDENCE
LOWEST PRECEDENCE
EXPRESSION EVALUATION IS LEFT TO RIGHT.
28
Introduction to Computer Sciencewith Fadi P. Deek
THE 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).
29
Introduction to Computer Sciencewith Fadi P. Deek
ONE ALTERNATIVE
if (logical_expression)
statement_1;
if (total_grade >= class_average) cout << "passing!" << endl;
30
Introduction to Computer Sciencewith Fadi P. Deek
LOGIC FLOW OF if WITHONE ALTERNATIVE
statement_1
ENTER
EXIT
true
false
logical expression
31
Introduction to Computer Sciencewith Fadi P. Deek
TWO ALTERNATIVESif (logical expression)
statement_1a;
else
statement_1b;
if (total_grade >= class_average) cout << "passing!" << endl;else cout << "in trouble!!!" << endl;
32
Introduction to Computer Sciencewith Fadi P. Deek
LOGIC FLOW OF if WITHTWO ALTERNATIVES
statement_1a statement_1b
ENTER
EXIT
true falselogical expression
33
Introduction to Computer Sciencewith Fadi P. Deek
COMPOUND 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.
34
Introduction to Computer Sciencewith Fadi P. Deek
EXAMPLE:
if (total_grade >= class_average){ passing_grade = passing_grade + 1; cout << "passing!" << endl;}else{ weak_grade = weak_grade + 1; cout << "in trouble!!!" << endl;}
35
Introduction to Computer Sciencewith Fadi P. Deek
MULTIPLE ALTERNATIVESif (logical expression_1)
statement_1;
else if (logical expression_2)
statement_2;
.
.
else if (logical expression_n)
statement_n;
else
statement_n+1;
36
Introduction to Computer Sciencewith Fadi P. Deek
if (total_grade >= 90)
letter_grade = ‘A’;
else if (total_grade >= 80)
letter_grade = ‘B’;
else if (total_grade >= 70)
letter_grade = ‘C’;
else if (total_grade >= 60)
letter_grade = ‘D’;
else
letter_grade = ‘F’;
37
Introduction to Computer Sciencewith Fadi P. Deek
statement_1
ENTER
EXIT
true false
LOGIC FLOW OF if WITHMULTIPLE ALTERNATIVES
logical expression_1
statement_2a statement_2b
falselogical expression_2
true
38
Introduction to Computer Sciencewith Fadi P. Deek
statement_1a statement_1b
false
statement_2
ENTER
EXIT
true false
LOGIC FLOW OF if WITHMULTIPLE ALTERNATIVES
logical expression_1
logical expression_2
true
39
Introduction to Computer Sciencewith Fadi P. Deek
SEQUENTIAL if VERSUSNESTED if
STATEMENTSif (number > 0)
positive_num = positive_num + 1;
if (number < 0)
negative_num = negative_num + 1;
if (number == 0)
zero_num = zero_num + 1;
40
Introduction to Computer Sciencewith Fadi P. Deek
if (total_grade >= 90)
letter_grade = ‘A’;
else if (total_grade >= 80)
letter_grade = ‘B’;
else if (total_grade >= 70)
letter_grade = ‘C’;
else if (total_grade >= 60)
letter_grade = ‘D’;
else
letter_grade = ‘F’;
41
Introduction to Computer Sciencewith Fadi P. Deek
.
.
if (side_1 == side_2) && (side_2 == side_3)
cout << "Triangle is Equilateral" << endl;
else if (side_1 == side_2) || (side_2 == side_3) ||
(side_1 == side_3)
cout << "Triangle is Isosceles" << endl;
else
cout << "Triangle is Scalene" << endl;
.
.
COMPOUND if STATEMENTS
42
Introduction to Computer Sciencewith Fadi P. Deek
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.
THE while STATEMENT
43
Introduction to Computer Sciencewith Fadi P. Deek
while (logical_expression)
statement;
total = 0;counter = 1;
// the following is a loop
while (counter <= 10){ cout << "Enter grade: "; cin >> grade; total = total + grade; counter = counter + 1;}
44
Introduction to Computer Sciencewith Fadi P. Deek
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.
45
Introduction to Computer Sciencewith Fadi P. Deek
IMPLEMENTATION:// 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 <iostream.h> // 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
Introduction to Computer Sciencewith Fadi P. Deek
// 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
Introduction to Computer Sciencewith Fadi P. Deek
// Display user instructions Instruct_user (tax_bracket, tax);
// Enter number of repetitions cout << "Number of repetitions : ”; cin >> final;
counter = 1; while (counter <= final) { // Enter hours and rate cout << "Hours worked: "; cin >> hours; cout << "Hourly rate: "; cin >> rate;
// Compute gross salary gross = Compute_gross (hours, rate);
48
Introduction to Computer Sciencewith Fadi P. Deek
// Compute net salary net = Compute_net (gross, tax_bracket, tax);
// Print gross and net cout << "Gross salary is $" << gross << endl; cout << "Net salary is $" << net << endl; // Increment the counter counter ++; } // end of the while loop return;} // end of main ()
49
Introduction to Computer Sciencewith Fadi P. Deek
// Display user instructionsvoid Instruct_user (const float tax_bracket, // IN: maximum salary // for no deduction const float tax) // IN: tax amount{ cout << "This program computes gross and net salary." << endl; cout << "A tax amount of $" << tax << " is deducted for" << endl; cout << "an employee who earns more than $" << tax_bracket << endl << endl; cout << "First, enter how many times do you want to " << "repeat the calculations." << endl << endl; cout << "Then, enter hours worked and hourly rate" << endl; cout << "on separate lines after the prompts." << endl; cout << "Press <RETURN> after typing each number." << endl << endl; return;} // end of Instruct_user
50
Introduction to Computer Sciencewith Fadi P. Deek
// Find the gross salaryfloat 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 salaryfloat 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 > tax_bracket) return gross - tax; // deduct a tax amount else return gross; // deduct no tax} // end of Compute_net