Chapter 3 Assignment Page 1

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.

  1. Basic IF-THEN-ELSE pattern – the basic IF-THEN-ELSE pattern is used when a decision needs to be made and based on the outcome of the decision either the true path or false path is followed.
Decision and connector symbols – The decision symbol is a diamond shaped symbol. It indicates that, at a particular point in processing, a choice between two alternative paths, or sequences of instructions, is to be made.

wpe22.gif (1332 bytes)

                Decision Symbol
Once the computer has completed the steps on the true or false path, the paths come to a joining point at the end and are connected by the connector symbol.

wpe21.gif (895 bytes)

Connector Symbol

Flowcharting Example:

wpe23.gif (1936 bytes)





Pseudocode Example:

wpe25.gif (1033 bytes)

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.

Pseudocode – in the first chapter, all of the problems were solved using flowcharting. This chapter introduces pseudocode. Pseudocode consists of English-language statements that process the steps of a program in paragraph form. The number of IF statements and ENDIF statements should match. All statements on the true path and false path are indented four spaces. All IF, ELSE and ENDIF statements should line up. 2. Problems using the null ELSE pattern The null ELSE pattern is used when there is processing on the true path only. When the tested condition is not true, no special alternative action is required. The true path will perform some type of processing or output. The false path goes directly to the connector symbol. In pseudocode, the no-function condition is represented by enclosing the keyword ELSE in parentheses. It is immediately followed by the keyword ENDIF. NOTE: A null THEN clause exists, but to keep things simple, we will always convert null THEN logic to null ELSE logic by negating the condition being tested and reversing the yes and no labels on the paths.

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.

wpe2C.gif (1693 bytes)

 

In pseudocode:

wpe29.gif (1033 bytes)

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.