Lecture 8 - Nested IFs


Home


Objectives

Contents


1. Linear Nested IFs

The problems so far have required sequence or two-way selection. Two-way selection involved two alternatives e.g. perform different processing if price < 100 or alternatively price >= 100. Some problems have more than 2 alternatives that requiring multi-way decisions. Some typical problems are explored below:

Example 1: Discrete values

A club requires a program to calculate the cost of membership. The cost of membership depends on the membership type. Full members ("F") are charged $160 per year, Juniors ("J") are charged $80 per year, and Life members ("L") are charged $10 per year. Assume that only valid membership types ("F", "J" or "L") are input.

Example Runs

Run
Membership type (F = Full, J = Junior, L = Life) ? F
Annual Membership = $160
Run
Membership type (F = Full, J = Junior, L = Life) ? J
Annual Membership = $80
Run
Membership type (F = Full, J = Junior, L = Life) ? L
Annual Membership = $10

Check Understanding

Membership type = "F": Annual membership for Full member = $160
Membership type = "J": Annual membership for Junior member = $80
Membership type = "L": Annual membership for Life member = $10

Defining Diagram

Inputs Processing Output
membershipType Input membershipType
Determine membershipCost
Display membershipCost
membershipCost

Outline the Solution

This requires a 3 way decision - there are three possible legal values of the membershipType, each with a single corresponding membershipCost - there is no ambiguity regarding the appropriate membershipCost once the membershipType is known. This could be represented as the following table:

  membershipType
 "F" "J" "L"
membershipCost $160 $80 $10

Logically each of the membershipTypes can be selected and the corresponding membershipCost obtained. There are 3 paths through the logic, as shown in the diagram below:

 
 
 
 
 
 
 
 
 


Pseudo Code

clubMembership()
    Input membershipType
    IF membershipType = "F" THEN
        membershipCost = 160
    ELSE
        IF membershipType = "J" THEN
            membershipCost = 80
        ELSE
            membershipCost = 10
        ENDIF
    ENDIF
    Display membershipCost
STOP

Data Dictionary

Name Data Type Description
membershipCost Integer The annual cost of membership in whole $
membershipType String The type of membership. Values: "F"=Full, "J"=Junior, "L"=Life.

Desk Check

1  clubMembership()
2      Input membershipType
3      IF membershipType = "F" THEN
4          membershipCost = 160
5      ELSE
6          IF membershipType = "J" THEN
7              membershipCost = 80
8          ELSE
9              membershipCost = 10
10         ENDIF
11     ENDIF
12     Display membershipCost
13 STOP

Inputs: Membership type = "F". Correct Results: Annual membership for full member = $160

Line Number membershipCost membershipType Conditions Input/Output
1, 2   "F"   membershipType? F
3     "F" = "F" ? is T  
4 160      
11, 12       membershipCost = 160
13        

Note: When line numbers in a row for are 3 or more consecutive lines, they can optionally be abbreviated to a range using a hyphen e.g. 10, 11, 12 becomes 10 - 12

Inputs: Membership type = "J". Correct Results: Annual membership for junior member = $80

Line Number membershipCost membershipType Conditions Input/Output
1, 2   J   membershipType? J
3     "J" = "F" ? is F  
5, 6     "J" = "J" ? is T  
7 80      
10 - 12       membershipCost = 80
13        

Inputs: Membership type = "L". Correct Results: Annual membership for life member = $10

Line Number membershipCost membershipType Conditions Input/Output
1, 2   L   membershipType? L
3     "L" = "F" ? is F  
5, 6     "L" = "J" ? is F  
8, 9 10      
10 - 12       membershipCost = 10
13        

Write the Program

' Name: clubMembership
' Purpose: Calculate the cost of membership in a club
' Author: Tim Whitfort
Option Compare Database
Option Explicit
Sub Main()

    ' Declare variables
    Dim membershipCost As Long   ' The annual cost of membership in whole $
    Dim membershipType As String ' The type of membership. Values: "F"=Full, "J"=Junior, "L"=Life.

    ' Input the membership type
    membershipType = InputBox("Membership type (F = Full, J = Junior, L = Life) ? ")

    ' Determine the membershipCost from the membershipType
    If membershipType = "F" Then
        membershipCost = 160
    Else
        If membershipType = "J" Then
            membershipCost = 80
        Else
            membershipCost = 10
        End If
    End If

    ' Display membership cost
    Debug.Print "Annual membership cost $"; membershipCost
End Sub

Discussion

The above IF statements are called Nested-IFs. A Nested-IF is where an IF is inside another IF.

What changes need to be made if the membership type input by the user can be wrong (invalid) ?

Example 2: Ranges of Floating Point Values

The above problem used discrete values for the variable in the condition e.g. "F", "J", "L". Some problems base the condition on a range of values (e.g. income), as shown in the following problem.

Write a program to calculate the cost of a phone call. The cost of the call depends solely on the distance of the call as shown below.

Distance (km) Cost per minute ($)
0 to < 50 0.15
50 to < 200 0.20
200 to < 1000 0.30
1000+ 0.35

Example Runs

Run
Distance (km) ? 10.4
Duration (minutes) ? 5
Cost = $0.75
Run
Distance (km) ? 50
Duration (minutes) ? 10
Cost = $2.00
Run
Distance (km) ? 999.9
Duration (minutes) ? 10
Cost = $3.00
Run
Distance (km) ? 2000
Duration (minutes) ? 20
Cost = $7.00

Check Understanding

 
 
 
 
 

Defining Diagram

 
 
 
 
 
 

Outline the Solution

 
 
 
 
 

Pseudo Code

calcCallCost()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
STOP

Data Dictionary

Name Data Type Description
cost Double The cost of the phone call in $
costPerMinute Double The cost per minute of the phone call in $
distance Double The distance the call was over in km
duration Double The amount of time the call was for in minutes

Desk Check

Inputs: distance = 10.4, duration = 5. Correct Result: cost = 0.75

 
 
 
 
 
 
 
 
 

Inputs: distance = 999.9, duration = 10. Correct Result: cost = 3.00

 
 
 
 
 
 
 
 
 
 
 

Write the Program

' Name: calcCallCost
' Purpose: Calculate the cost of a phone call
' Author: Tim Whitfort
Option Compare Database
Option Explicit
Sub Main()

    ' Declare variables
    Dim cost As Double  ' The cost of the phone call in $
    Dim costPerMinute As Double ' The cost per minute of the phone call in $
    Dim distance As Double ' The distance the call was over in km
    Dim duration As Double ' The amount of time the call was for in minutes

    ' Input the distance of the call
    distance = InputBox("Distance (km) ? ")
    ' Input the duration of the call
    duration = InputBox("Duration (minutes) ? ")
 
 
 
 
 
 
 
 
 
 
 
 
 
   ' Calculate the call cost
    cost = duration * costPerMinute

    ' Display the cost of the call
    Debug.Print "Cost $"; Format(cost, "0.00")
End Sub

Example 3: Ranges of Integer Values

The above problem used a range of floating point values for the variable in the condition. The following uses ranges of integer values in the conditions (income), which  means that the conditions can be expressed using <= rather than <.

Write a program to calculate the tax for an employee. The tax rates are as shown in the following table:

Taxable Income Tax
$0 to $6,000 Nil
$6,001 to $20,000 17c for each $1 over $6,000
$20,001 to $50,000 $2,380 plus 30c for each $1 over $20,000
$50,001 to $60,000 $11,380 plus 42c for each $1 over $50,000
Over $60,000 $15,580 plus 47c for each $1 over $60,000

Example Runs

Run
Income in whole dollars ($) ? 5000
Tax = $0
Run
Income in whole dollars ($) ? 20000
Tax = $2380
Run
Income in whole dollars ($) ? 55001
Tax = $13480.42

Check Understanding

 
 
 

Defining Diagram

Inputs Processing Output
income Input income
Determine tax
Display tax
tax

Outline the Solution

This is a 5-way decision. There are 5 tax brackets.

 
 
 
 
 
 

Pseudo Code

calcTax()
    Input income
    IF income <= 6000 THEN
        tax = 0
    ELSE
        IF income <= 20000 THEN
            tax = (income - 6000) * 0.17
        ELSE
            IF income <= 50000 THEN
                tax = 2380 + (income - 20000) * 0.30
            ELSE
                IF income <= 60000 THEN
                    tax = 11380 + (income - 50000) * 0.42
                ELSE
                    tax = 15580 + (income - 60000) * 0.47
                ENDIF
            ENDIF
        ENDIF
    ENDIF
    Display tax
STOP

Data Dictionary

Name Data Type Description
income Integer The net income of the employee on whole $
tax Double The tax payable on the net income in $

Discussion

Why are the tax brackets dealt with in increasing order of income? Why not start with the tax bracket for an income of $6,001 to $20,000?

Desk Check

1  calcTax()
2      Input income
3      IF income <= 6000 THEN
4          tax = 0
5      ELSE
6          IF income <= 20000 THEN
7              tax = (income - 6000) * 0.17
8          ELSE
9              IF income <= 50000 THEN
10                 tax = 2380 + (income - 20000) * 0.30
11             ELSE
12                 IF income <= 60000 THEN
13                     tax = 11380 + (income - 50000) * 0.42
14                 ELSE
15                     tax = 15580 + (income - 60000) * 0.47
16                 ENDIF
17             ENDIF
18         ENDIF
19     ENDIF
20     Display tax
21 STOP

Inputs: income = 5000. Correct results: tax = 0

Line Number income tax Conditions Input/Output
1, 2 5000     income ? 5000
3     5000 <= 6000  ? is T  
4   0    
19, 20       tax = 0
21        

Inputs: income = 20000. Correct results: tax = 2380

Line Number income tax Conditions Input/Output
1, 2 20000     income ? 20000
3     20000 <= 6000 ? is F  
5, 6     20000 <= 20000 ? is T  
7   (20000 - 6000) * 0.17 = 2380    
18 - 20       tax = 2380
21        

Inputs: income = 55001. Correct results: tax = 13480.42

Line Number income tax Conditions Input/Output
1, 2 55001     income ? 55001
3     55001 <= 6000 ? is F  
5, 6     55001 <= 20000 ? is F  
8, 9     55001 <= 50000 ? is F  
11, 12     55001 <= 60000 ? is T  
13   11380 + (55001 - 50000) * 0.42 = 13480.42    
16 - 20       tax = 13480.42
21        

Write the Program

(Done in the tutorial)


Key Points

Further Reading


Written by Tim Whitfort.