Академический Документы
Профессиональный Документы
Культура Документы
Objectives
Symbols
IF condition THEN
statementA
ELSE
statementB
ENDIF
Simple Program Design, Fourth Edition Chapter 4 7
1 Simple Selection (Simple IF
Statement)
• The IF ... THEN ... ELSE
• Example:
IF condition THEN
statementA
:
:
ENDIF
• Example
• C Desk checking
• Two sets of valid characters will be used to check the
algorithm; the characters k, b and g as the first set,
and z, s and a as the second
• Input data:
• C Desk checking
• Expected results:
First data set Second data set
char_1 b a
char_2 g s
char_3 k z
• C Desk checking
• Desk check table:
Statement # Char_1 Char_2 Char_3 temp
First pass
1,2 k b g
3 b k k
4 g k k
5
6 output output output
Second pass
1,2 z s a
3 s z z
4 a z z
5 a Simple Program
s Design, Fourth Edition Chapter s
4 25
6 output output output
Example 4.2 Process Customer
Record
• A program is required to read a customer’s name, a
purchase amount, and a fax code. The tax code has been
validated and will be one of those listed on page 43 of the
textbook. The program must then compute the sales tax
and the total amount due, and print the customer’s name,
purchase amount, sales tax, and total amount due
• A Defining diagram (shown on page 43)
• B Solution algorithm
• The solution algorithm requires a linear nested IF statement
to calculate the sales tax illustrated in the code on page 43
of the textbook
• Defining Diagram
integer_1 20 100
integer_2 2 0
expected results:
sum 22 100
difference 18 100
product 40 0
quotient 10 message
First Prompt/Get 20 2
pass sum 22
difference 18
product 40
IF 10
difference 100
product 0
IF message
Simple Program Design, Fourth Edition Chapter 4 42
Display yes yes yes message
Ex. 2
number_1 10 20
number_2 4 5
code 2 6
expected results:
First data set Second data set
result 6 0
message blank invalid integer code
CASE 6
IF true
Second Get 20 5 6