Nested Decision Structure Can Be Used To Test More Than One Condition

r

Go ouiside.

The flowchart in the figure starts with a sequence structure. Assuming you have an outdoor thermometer in your window, the first step is Go to the window, and the next step is Read thermometer. A decision structure appears next, testing the condition Cold outside. If this is true, the action wear a coat is performed. Another sequence structure appears next. The step Open the door is performed, followed by Go outside.

Quite often, structures must be nested inside other structures. For example, look at the partial flowchart in Figure 4-13. It shows a decision structure with a sequence structure nested inside it. The decision structure tests the condition Cold outside. If that condition is true, the steps in the sequence structure are executed.

flgwre 4-12» A sequence structure nested inside a decision structure flgwre 4-12» A sequence structure nested inside a decision structure

Sequence structure

You can also nest decision structures inside other decision structures. In fact, this is a common requirement in programs that need to test more than one condition. For example, consider a program that determines whether a bank customer qualifies for a loan. To qualify, two conditions must exist: (1)the customer must earn at least \$30,000 per year, and (2) the customer must have been employed at his or her current job for at least two years. Figure 4-14 shows a flowchart for an algorithm that could be used in such a program. Assume that the salary variable is assigned the customer's annual salary, and the years_on_job variable is assigned the number of years that the customer has worked on his or her current job.

132 Chapter 4 Decision Structures and Boolean Logic figure 4-84 A nested decision structure

If we follow the flow of execution, we see that the condition salary >= 30000 is tested. If this condition is false, there is no need to perform further tests; we know that the customer does not qualify for the loan. If the condition is true, however, we need to test the second condition. This is done with a nested decision structure that tests the condition years_on_job >= 2. If this condition is true, then the customer qualifies for the loan. If this condition is false, then the customer does not qualify. Program 4-5 shows the code for the complete program.

Program 4-5 (loan_qualifier.py)

# This program determines whether a bank customer - # qualifies for a loan.

# Get the customer's annual salary.

salary = input('Enter your annual salary: 1 )

# Get the number of years on the current job. years-on-job = input('Enter the number of 1 + \

'years on your current job: ')

# Determine whether the customer qualifies, if salary * 30000.0:

print 'You qualify for the loan.1 else:

17 print 'You must have been on your current1

print 'job for at least two years to qualify.1

else:

2C print 'You must earn at least \$30,000 per year'

print 'to qualify.1

Program Output (with input shown in bold) Enter your annual salary: 35000 [Enter]

Enter the number of years on your current job: 1 [Enter] You must have been on your current job for at least two years to qualify.

Program Output (with input shown in bold) Enter your annual salary: 25000 [Enter]

7-;r>t&E r.hs i-iimbcr of years on your cu rf:;-jt job: 5 [Enter] You must earn at least \$30,000 per year to qualify.

Program Output (with input shown in bold) Enter your annual salary: 35000 [Enter]

.S^tic r.he r:f yii.^'f: r,r. your cur ent "job: 5 [Enter]

You qualify for the loan.

Look at the if- else statement that begins in line 13. It tests the condition salary >= 3 0000.0. If this condition is true, the i f-else statement that begins in line 14 is executed. Otherwise the program jumps to the else clause in line 19 and executes the two print statements in lines 20 and 21. The program then leaves the decision structure and the main function ends.

It's important to use proper indentation in a nested decision structure. Not only is proper indentation required by the Python interpreter, but it also makes it easier for you, the human reader of your code, to see which actions are performed by each part of the structure. Follow these rules when writing nested i f statements:

• Make sure each else clause is aligned with its matching i f clause. This is shown in Figure 4-15.

* Make sure the statements in each block are consistently indented. The shaded parts of Figure 4-16 show the nested blocks in the decision structure. Notice that each statement in each block is indented the same amount.

Figure 4-15 Alignment cf i f and else clauses

This i f and else-go together.

print 'You qualify for the loan.'

print 'You must have been on your current' ,[seprint 'job for at least two years to qualify.

print 'You must earn at least \$30,000 per year' print 'to qualify.'

Figure 4-16 Nested blocks if salary >= 30000.0:

r r i« 'V:-- »• isr BMK mmm or ggMI »■—■ — '

else:

print 'You must earn at .lease \$30,000 per year' print 'to qualify.. 1

Testing a Series of Conditions

In the previous example you saw how a program can use nested decision structures to test more than one condition. It is not uncommon for a program to have a series of conditions to test, and then perform an action depending on which condition is true. One way to accomplish this it to have a decision structure with numerous other decision structures nested inside it. For example, consider the program presented in the following In the Spotlight section.

0 0

Responses

• amaranth
What are nested decision in python?
7 years ago