IF-THEN-ELSE Control Structure
This chapter introduces the second control structure. You will use the information you learned in the previous chapter and build on the information in this chapter. In the previous chapter all of problems accepted input data, performed processing steps and output information. Each of the input records followed the same processing steps. The IF-THEN-ELSE control structure is used when a decision needs to be made. The IF-THEN-ELSE may be used when the processing steps each input record needs to follow may be different or the input records may need to output different information.
For example: if you are designing a payroll program, the processing will be different depending upon whether, the person worked overtime hours or their regular hours.
Decision Symbol
Connector Symbol
Flowcharting Example:
Pseudocode Example:
The connector symbol is used in representing the decision-making logic. It acts as a collector, emphasizing that the IF-THEN-ELSE pattern has only one entry point and one exit point. NOTE: each decision symbol has one connector symbol. You should count the number of connector symbols and decision symbol to make sure they match. If the true path statement is followed, the false path statements are skipped. If the false path statement is followed, the true path statements are skipped.
The logical operators are: = (equal), <> (not equal), < (less than), > (greater than), >= (greater than or equal to), and <= (less than or equal to). To change a null THEN statement to a null ELSE statement, you need to reverse the logical operator. For example the reverse of greater than is less than. There is only one problem in the chapter that covers the null else statement. An additional example: print out all employees that earn more than $30,000. A null else statement is needed to output only those employees.
In pseudocode:
Exercises:
Problem #8 - Quotes need to
be used around the "U" and "S". Placing the quotes around the characters
means you want to compare the input values to the character "U" and "S"
. If quotes are not placed around the characters, then memory locations
named U and S will be created. Each memory location will contain data.
Problem #9 - In design a percent
can be written as 1% or as a decimal .01.
Problem #10 - Labels are used on the output because otherwise, the output of item# and computer inventory value will output only two numbers on the report. A person reading the report will not know what the number mean. Labels are used instead of headings since the output values can be different.
Problem #11 – This is the easiest problem to start with.
Problem # 12 – This is the only
Null ELSE problem in the chapter. The term real new balance confuses the
students. It is actually new balance with a $5 deduction.