Top Banner
Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured and Object- Oriented Design, 7e
69

Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Dec 16, 2015

Download

Documents

Tracy Ford
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Chapter 3

IFTHENELSEControl Structure

© 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Marilyn Bohl/Maria RynnTools for Structured and Object-Oriented Design, 7e

Page 2: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Introduction

In a SIMPLE SEQUENCE the steps are carried out in a sequential manner. It may be desirable to vary the sequence of

processing steps carried out within a solution algorithm.

Page 3: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Billing Problem (Flowchart)

Page 4: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Billing Example

Decision symbol The diamond-shaped symbol. A choice between two alternative paths, or

sequences of instructions, is made.

Page 5: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Pseudocode

Pseudocode Similar to some high-level programming

languages, but it does not require that we follow strict rules as we would if actually writing a program.

Page 6: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Pseudocode—Introduction

Page 7: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

IFTHENELSE Control Structure

IFTHENELSE control structure A small, circular symbol, called a connector

symbol, is used to represent the decision-making logic within the IFTHENELSE pattern

Page 8: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

IFTHENELSE--Generic

Page 9: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Time Card Problem

Page 10: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Time Card Example

No-function condition—null ELSE The no-function condition is represented by

enclosing the key-word ELSE in parentheses.

Page 11: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Sample Problem 3.1

Payroll Problem Compute the pay for an employee. Regular pay will be computed as hours (through 40)

times rate. Overtime pay (1.5 times hours times rate) for all

hours worked over 40.

Page 12: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Payroll Problem (Flowchart)

Page 13: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Payroll Problem (Flowchart) cont.

Page 14: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Payroll Problem (Pseudocode)

Page 15: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Finding the Smallest Number (Flowchart)

Page 16: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Finding the Smallest Number (Pseudocode)

Page 17: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Sample Problem 3.3

Bank Problem Compute the new balance in a customer's bank

account. A deposit (code of 1) or withdrawal (code of 2)

Page 18: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Bank Problem (Flowchart)

Page 19: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Bank Problem (Flowchart) cont.

Page 20: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Bank Problem (Pseudocode)

Page 21: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Character-String Constant

The following information would be output as a result of 3 being input and the WRITE statement:

Designate character-string by enclosing it in single or double quotation marks, distinguishing it from a variable name.

If we included the variable name CODE within the quotation marks:

Page 22: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Character-String Constant

Assign the value of a character-string constant to a variable by means of a statement.

Use a character-string constant in a decision statement.

Compared to the letter D.

Compared to the value of the variable D.

Page 23: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Sample Problem 3.4

Sales Problem The commission rate is based on two factors, the

amount of sales and the class to which the sales person belongs.

Page 24: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Sample Problem 3.4

Page 25: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Sales Problem (Flowchart)

Page 26: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Sales Problem (Pseudocode)

Page 27: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Sales Problem (Pseudocode) cont.

Page 28: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Chapter 4

DOWHILE Control Structure—Counter-

Controlled Loops© 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Marilyn Bohl/Maria RynnTools for Structured and Object-Oriented Design, 7e

Page 29: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Introduction

Program loop A sequence of processing steps that may be

repeated.

Page 30: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Adding Six Numbers (No Loop)

Page 31: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Introduction

Practical limitations of this approach What if you revise this to compute and output the

sum of 100 numbers?

Page 32: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Calculator Simulation (No Loop)

Using a calculator:

Page 33: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Calculator Simulation (Loop)

Page 34: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Adding Six Numbers

Page 35: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Adding Six Numbers cont.

Page 36: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Problem (Adding Six Numbers)

Preparation symbol An operation performed on data in preparation for a

major sequence of operations. Initialization step.

Accumulator A variable holding the sum of a group of values. Counter is a special type of accumulator.

Adds or accumulates by a constant amount. Partial sum—ACCUM is used to hold the sum of

the numbers so far.

Page 37: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Adding Six Numbers--Simulation

Page 38: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Adding Six Numbers--Simulation

Page 39: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

The DOWHILE Loop

Connector symbol A collector at the beginning of the loop.

DOWHILE Loop Leading-decision program loop

The test to determine whether the loop should be executed or exited is encountered immediately upon entering the loop.

If the tested condition is not true the first time it is tested, the remaining steps in the loop are not executed at all.

Page 40: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

The DOWHILE Loop

Properly formed DOWHILE loop: Place the loop test before any other step within the loop

(leading decision). Place the loop steps in the YES path of the loop test. Indicated that the loop will exit in the NO path of the loop

test.

Page 41: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

DOWHILE Loop-Generic

Page 42: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Counter-Controlled Loops

Counter-controlled loop The algorithm clearly shows the number of times

the loop steps will be done. The loop is controlled by a counter. The number of times the loop will be executed is known

or preset. COUNT is often referred to as the loop control

variable.

Page 43: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Simple Counter-Controlled Loops--Generic

Page 44: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Payroll with Counter Loop

Payroll problem from Chapter 3 except for 10 employees.

Page 45: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Payroll Problem—10 Employees (Flowchart)

Page 46: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Payroll Problem—10 Employees (Flowchart) cont.

Page 47: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Payroll Problem—10 Employees (Pseudocode)

Page 48: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Averaging Problem with Counter Loop

Problem A computer program that will compute and print a

student's term average. Each student has five scores.

Page 49: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Averaging Problem (Flowchart)

Page 50: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Averaging Problem (Flowchart) cont.

Page 51: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Averaging Problem (Pseudocode)

Page 52: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Header Record Logic

Header record logic A more flexible approach to loop control The number of loop iterations can vary with each

program execution. Header record

Specifies how many additional input records will follow.

Page 53: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Header Record Logic--Generic

Page 54: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Payroll with Header Record

Problem: Sample Problem 4.1 (Payroll) including a header

record. Input record contains the number of employees.

Page 55: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Payroll Problem with Header Record (Flowchart)

Page 56: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Payroll Problem with Header Record (Flowchart) cont.

Page 57: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Payroll Problem with Header Record (Pseudocode)

Page 58: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Averaging Problem with Header Record

Problem: The number of individual scores that must be added

will depend on the number of assignments completed.

Page 59: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Averaging Problem with Header Record (Flowchart)

Page 60: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Averaging Problem with Header Record (Flowchart) cont.

Page 61: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Averaging Problem with Header Record (Pseudocode)

Page 62: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

The No-Data Condition

What if N is less than or equal to 0? No path will be taken immediately. The step following involves a division by N. A division by 0 will be the result.

Page 63: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Averaging Problem with No-Data Test (Flowchart)

Page 64: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Averaging Problem with No-Data Test (Flowchart) cont.

Page 65: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Averaging Problem with No-Data Test (Pseudocode)

Page 66: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Proper Programs

Any solution algorithm can be expressed using the three basic patterns of logic. SIMPLE SEQUENCE IFTHENELSE DOWHILE

Page 67: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Proper Programs

Building-block concept Each basic pattern is characterized by a single

point of entrance and a single point of exit. Contained patterns are nested. A solution algorithm should have only one entry

point and only one exit point. A program that can be viewed as a single statement

is called a proper program. The algorithms presented thus far are all examples

of proper programs.

Page 68: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Enrichment

Visual Basic Graphical user interface for the problem involving

the addition of six numbers.

Page 69: Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.

Enrichment

If the user clicks the Cancel button instead of the OK button, no value will be assigned to decNumber. This could cause an error. If the user enters a non-numeric value in the input

box, another type of error may occur.