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
Dec 16, 2015
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 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.
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
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.
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)
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.
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
Practical limitations of this approach What if you revise this to compute and output the
sum of 100 numbers?
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.
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.
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.
Averaging Problem with Counter Loop
Problem A computer program that will compute and print a
student's term average. Each student has five scores.
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.
Payroll with Header Record
Problem: Sample Problem 4.1 (Payroll) including a header
record. Input record contains the number of employees.
Averaging Problem with Header Record
Problem: The number of individual scores that must be added
will depend on the number of assignments completed.
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.
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.