Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All...

69
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

Transcript of Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All...

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

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.

Billing Problem (Flowchart)

Billing Example

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

sequences of instructions, is made.

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.

Pseudocode—Introduction

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

IFTHENELSE--Generic

Time Card Problem

Time Card Example

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

enclosing the key-word ELSE in parentheses.

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.

Payroll Problem (Flowchart)

Payroll Problem (Flowchart) cont.

Payroll Problem (Pseudocode)

Finding the Smallest Number (Flowchart)

Finding the Smallest Number (Pseudocode)

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)

Bank Problem (Flowchart)

Bank Problem (Flowchart) cont.

Bank Problem (Pseudocode)

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:

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.

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.

Sample Problem 3.4

Sales Problem (Flowchart)

Sales Problem (Pseudocode)

Sales Problem (Pseudocode) cont.

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

Introduction

Program loop A sequence of processing steps that may be

repeated.

Adding Six Numbers (No Loop)

Introduction

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

sum of 100 numbers?

Calculator Simulation (No Loop)

Using a calculator:

Calculator Simulation (Loop)

Adding Six Numbers

Adding Six Numbers cont.

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.

Adding Six Numbers--Simulation

Adding Six Numbers--Simulation

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.

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.

DOWHILE Loop-Generic

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.

Simple Counter-Controlled Loops--Generic

Payroll with Counter Loop

Payroll problem from Chapter 3 except for 10 employees.

Payroll Problem—10 Employees (Flowchart)

Payroll Problem—10 Employees (Flowchart) cont.

Payroll Problem—10 Employees (Pseudocode)

Averaging Problem with Counter Loop

Problem A computer program that will compute and print a

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

Averaging Problem (Flowchart)

Averaging Problem (Flowchart) cont.

Averaging Problem (Pseudocode)

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.

Header Record Logic--Generic

Payroll with Header Record

Problem: Sample Problem 4.1 (Payroll) including a header

record. Input record contains the number of employees.

Payroll Problem with Header Record (Flowchart)

Payroll Problem with Header Record (Flowchart) cont.

Payroll Problem with Header Record (Pseudocode)

Averaging Problem with Header Record

Problem: The number of individual scores that must be added

will depend on the number of assignments completed.

Averaging Problem with Header Record (Flowchart)

Averaging Problem with Header Record (Flowchart) cont.

Averaging Problem with Header Record (Pseudocode)

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.

Averaging Problem with No-Data Test (Flowchart)

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

Averaging Problem with No-Data Test (Pseudocode)

Proper Programs

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

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.

Enrichment

Visual Basic Graphical user interface for the problem involving

the addition of six numbers.

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.