500 likes | 685 Views
CONTROL STRUCTURES: SEQUENTIAL, SELECTIVE, AND REPETITIVE. THREE CATEGORIES OF CONTROL STRUCTURES: SEQUENTIAL SELECTIVE REPETITIVE. SEQUENTIAL. CONTROL FLOWS FROM STATEMENT_1 TO STATEMENT_2 AND SO ON TO STATEMENT_N. statement_1. statement_2. statement_n. SELECTIVE.
E N D
THREE CATEGORIES OF CONTROL STRUCTURES: • SEQUENTIAL • SELECTIVE • REPETITIVE
SEQUENTIAL CONTROL FLOWS FROM STATEMENT_1 TO STATEMENT_2 AND SO ON TO STATEMENT_N.
statement_1 statement_2 . . statement_n
SELECTIVE CONTROL FOLLOWS ONE OF SEVERAL ALTERNATIVES OF LOGIC FLOW BASED ON A CONDITION.
ENTER true false logical expression process process EXIT
REPETITIVE CONTROL ALLOWS FOR THE REPETITION OF A CERTAIN TASK BASED ON A CONDITION.
ENTER true process logical expression false EXIT
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.
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.
2. 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
3. RELEVANT FORMULAE gross pay = hourly rate x hours worked net pay = gross pay - deductions
DESIGN: 1. STRUCTURE CHART PAYROLL SYSTEM LEVEL 0 LEVEL 1 COMPUTE GROSS PAY DISPLAY INSTRUCTIONS GET DATA COMPUTE NET PAY DISPLAY RESULTS
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 PAY • DATA RECEIVED: hours, rate • INFORMATION RETURNED: gross • LOGIC: THE HOURS ARE MULTIPLIED BY THE PAY • RATE TO PRODUCE THE GROSS PAY.
3. ALGORITHM: • 1. DISPLAY USER INSTRUCTIONS • 2. WHILE COUNTER <= FINAL • 2.1. GET DATA • 2.2. COMPUTE GROSS PAY • 2.3. COMPUTE NET PAY • 2.3.1. IF GROSS PAY > TAXBRACKET THEN • DEDUCT TAX • ELSE • DEDUCT NO TAX • 2.4. DISPLAY RESULTS
NEW CONCEPTS LOGICAL EXPRESSIONS, SELECTION, AND REPETITION
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).
LOGICAL EXPRESSIONS MAY BE FORMED BY USING COMBINATIONS OF TWO KINDS OF OPERATORS: RELATIONAL AND LOGICAL.
OPERATORMEANING ==EQUAL TO != NOT EQUAL TO < LESS THAN <= LESS THAN OR EQUAL TO > GREATER THAN >= GREATER THAN OR EQUAL TO RELATIONAL OPERATORS
OPERATORMEANING &&AND || OR ! NOT LOGICAL OPERATORS
LOGICAL EXPRESSIONS ARE MADE UP OF VARIABLES, CONSTANTS AND OPERATORS. operand operator operand
EXAMPLES: (counter <= FINAL) (class_average < 70.0) total_grade >= class_average hours_worked <= 35.0 divisor == 0 (angle1 + angle2 + angle3) != 180
(midterm_grade >= 90) && (final_grade >= 90) (side_1 == side_2) || (side_2 == side_3) ! (class_average >= 70.0)
&& TRUTH TABLE operandoperatoroperandoutcome 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 operandoperatoroperandoutcome 1(true) || 1(true) 1(true) 1(true) || 0(false) 1(true) 0(false) || 1(true) 1(true) 0(false) || 0(false) 0(false)
! TRUTH TABLE operatoroperandoutcome ! 1(true) 0(false) ! 0(false) 1(true)
PRECEDENCE OF OPERATORS (REVISITED) ! * / % + - < <= >= > == != && || = HIGHEST PRECEDENCE LOWEST PRECEDENCE EXPRESSION EVALUATION IS LEFT TO RIGHT.
ifSTATEMENT 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). THE if STATEMENT
ONE ALTERNATIVE if (logical_expression) statement_1; if (total_grade >= class_average) cout << "passing!" << endl;
LOGIC FLOW OF if WITHONE ALTERNATIVE ENTER true logical expression false statement_1 EXIT
TWO ALTERNATIVES if (logical expression) statement_1a; else statement_1b; if (total_grade >= class_average) cout << "passing!" << endl; else cout << "in trouble!!!" << endl;
LOGIC FLOW OF if WITHTWO ALTERNATIVES ENTER false true logical expression statement_1a statement_1b EXIT
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.
EXAMPLE: if (total_grade >= class_average) { passing_grade = passing_grade + 1; cout << "passing!" << endl; } else { weak_grade = weak_grade + 1; cout << "in trouble!!!" << endl; }
MULTIPLE ALTERNATIVES if (logical expression_1) statement_1; else if (logical expression_2) statement_2; . . else if (logical expression_n) statement_n; else statement_n+1;
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’;
LOGIC FLOW OF if WITHMULTIPLE ALTERNATIVES ENTER false logical expression_1 true true logical expression_2 false statement_2a statement_2b statement_1 EXIT
LOGIC FLOW OF if WITHMULTIPLE ALTERNATIVES ENTER false logical expression_1 true true logical expression_2 false statement_1a statement_1b statement_2 EXIT
SEQUENTIAL if VERSUSNESTED if STATEMENTS if (number > 0) positive_num = positive_num + 1; if (number < 0) negative_num = negative_num + 1; if (number == 0) zero_num = zero_num + 1;
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’;
COMPOUND if STATEMENTS . . 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; . .
THE REPETITION OF TASKS IN A PROGRAM IS CALLED LOOPING. WITH THE whileLOOP, 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
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; }
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.
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
// 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
// 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);
// 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 ()
// Display user instructions void 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
// 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 > tax_bracket) return gross - tax; // deduct a tax amount else return gross; // deduct no tax } // end of Compute_net