Selection Control Structure
Selection Control Structure
At first the above IF looks just like the previous IF. However, the
( ) changes how we interpret it. The record will be deleted if the
account blance is zero, AND one of the following is true: The
record code is 23 OR the update code is delete.) So, the record is
delete IF the record code is 23 AND the account balance is zero.
OR, the record will also be deleted IF the update code is delete and
the account balance is zero.
The NOT operator
add 1 to young_fem_pt_students
ENDIF {age >}
ENDIF {gender = male}
ENDIF {full time}
B Solution algrithm
Here is the algorithm
Read_three_characters
Prompt the operator for char_1, char_2,
char_3
Get char_1. char_2, char_3
IF char_1 > char_2 THEN
temp = char_1
char_1 = char_2
char_2 = temp
ENDIF
IF char_2 > char_3 THEN
temp = char_2
char_2 = char_3
char_3 = temp
ENDIF
IF char_1 > char_2 THEN
temp = char_1
char_1 = char_2
char_2 = temp
ENDIF
output to the screen char_1, char_2, char_3
END
C Desk checking
Now we choose some test data (characters)
B Solution algorithm
Here is the algorithm.
Process_customer_record
Read cust_name, purch_amt, tax_code
IF tax_code = 0 THEN
sales_tax = 0
ELSE
IF tax_code = 1 THEN
sales_tax = purch_amt * 0.03
ELSE
IF tax_code = 2 THEN
sales_tax =
purch_amt * 0.05
ELSE
sales_tax =
purch_amt * 0.07
ENDIF
ENDIF
ENDIF
total_amt = purch_amt + sales_tax
Print cust_name, purch_amt, sales_tax,
total_amt
END
C Desk checking
Now we think up some test data.
B Solution algorithm
First here is how a boolean variable works.
increment counter_B
ELSE
IF record_code = 'C' THEN
increment counter_C
ELSE
increment error_counter
ENDIF
ENDIF
ENDIF
Here is the general form of a CASE statement.
CASE OF single variable
value_1 :statement block_1
value_2 :statement block_2
.
.
.
value_n :statement block_n
value_other: statement block_other
ENDCASE
It has a variable of type integer, whose value determines
which statement block to execute. The value_other is
executed if none of the specified values are found. The
CASE statement can test many, many conditions. The
Tovin the Maze Solving Robot program has a CASE
statement with over 100 conditions being tested!
Here is the four condition If from above
replaced with a CASE statement.
CASE OF record_code
'A' :increment counter_A
'B' :increment counter_B
B Solution algorithm
Here is algorithm
Process_customer_record
Read cust_name, purch_amt, tax_code
CASE OF tax_code
0: sales_tax = 0
1: sales_tax = purch_amt * 0.03
2: sales_tax = purch_amt * 0.05
3: sales_tax = purch_amt * 0.07
ENDCASE
total_amt = purch_amt + sales_tax
Print cust_name, purch_amt, sales_tax,
total_amt
END
C Desk checking