Nested If exercise

Given the indicated sets of values, what would output would be produced for each of the following?

A)

blnHonorStudent = False

If sngGPA > 3.5 Then

    If intHoursCompleted >= 15 Then
        blnHonorStudent =
True
    End If

End If


Msgbox( blnHonorStudent)



   sngGPA     intHoursCompleted     Output

1) 3.0                     17                          False

Since 3.06 IS NOT > 3.5 control will pass to the statement following the second End If and “False” (the original value stored in blnHonorStudent) will be displayed.



2) 3.5                     15                          false

Since 3.5 IS NOT > 3 control will pass to the statement following the second End If and “False” (the original value stored in blnHonorStudent) will be displayed.



3) 4.0                     12                          false

Since 4.0 IS > 3.5 it will then test intHoursCompleted. Since 12 IS NOT greater than 15 blnHonorStudent will remain unchanged. Control will fall to the statement following the first End If. There are no statements so control will pass to the statement following the second End If and “False” (the original value stored in blnHonorStudent) will be displayed.



4) 3.6                     18                          True

Since 3.6 IS > 3.5 it will then test intHoursCompleted. Since 18 IS greater than 15 blnHonorStudent will be assigned True. Control will fall to the statement following the first End If. There are no statements so control will pass to the statement following the second End If and “True” will be displayed.





B)

sngBonus = 0

If sngSales > 300000 Then

    If intYearsEmployed > 2 Then
        sngBonus = 1000
    Else
        sngBonus = 500
    End If

End If


Msgbox( sngBonus)



   sngSales      intYearsEmployed       Output

1) 305,000               3                          1000

Since 305000 (sngSales) IS GREATER THAN 300000 control will pass to the second If statement.

Since 3 (intYearsEmployed) IS GREATER THAN 2 sngBonus is assigned the value 1000. Control will then fall to the statement following the first End If. . There are no statements to execute. Control will then pass to the statement following the second End If and 1000 will be displayed.



2) 200,000                4                          0

Since 200000 (sngSales) IS NOT GREATER THAN 300000 and there is no Else branch for this If statement control will then pass to the statement following the second End If (Each End If is paired with the most recent unmatched If statement) and 0 (sngBonus remains unchanged) will be displayed.



3) 325,000                1                          500

Since 325000 (sngSales) IS GREATER THAN 300000 control will pass to the second If statement.

Since 1 (intYearsEmployed) IS NOT GREATER THAN 2 the Else branch is followed and sngBonus is assigned the value 500. Control will then fall to the statement following the first End If. . There are no statements to execute. Control will then pass to the statement following the second End If and 500 will be displayed.



4) 300,000                2                          0

Since 300000 (sngSales) IS NOT GREATER THAN 300000 and there is no Else branch for this If statement control will then pass to the statement following the second End If (Each End If is paired with the most recent unmatched If statement) and 0 (sngBonus remains unchanged) will be displayed.



5) 300,100                2                          500

Since 300100 (sngSales) IS GREATER THAN 300000 control will pass to the second If statement.

Since 2 (intYearsEmployed) IS NOT GREATER THAN 2 the Else branch is followed and sngBonus is assigned the value 500. Control will then fall to the statement following the first End If. . There are no statements to execute. Control will then pass to the statement following the second End If and 500 will be displayed.


C)

If intAge > 17 Then

    If blnRegistered Then
        lblMessage.Text = "
You may vote
   
Else
        lblmessage.Text = “
You must register to vote
   
End If

Else

    lblmessage.Text = “
You are too young to vote
End If


intAge         blnRegistered         Output

1) 17                False                  You are too young to vote

Since 17 (intAge) IS NOT GREATER THAN 17 control will pass to the second Else statement and lblmessage.Text will be assigned the value “You are too young to vote


2) 16                False                  You are too young to vote

Since 16 (intAge) IS NOT GREATERTHAN 17 control will pass to the second Else statement and lblmessage.Text will be assigned the value “You are too young to vote


3) 23                True                  You may vote

Since 23 (intAge) IS GREATER THAN 17 control will pass to the nested If  statement (If blnRegistered Then) which is True. Control will pass to the statement  lblmessage.Text = “You may vote”.


4) 45                False                  You must register to vote

Since 45 (intAge) IS GREATER THAN 17 control will pass to the nested If  statement (If blnRegistered Then) which is False. Control will pass to the first Else statement  lblmessage.Text = “You must register to vote”.


D)

If sngAmtWithdrawal <= sngBalance Then

    If (sngTotRecentWithdrawal + sngAmtWithdrawal) < 500 Then
       sngBalance = sngBalance - sngAmtWithdrawal
       sngTotRecentWithdrawal = sngTotRecentWithdrawal + sngAmtWithdrawal
   
Else
       sngAmtWithdrawal = 0
   
End If

End If


Msgbox( "
Amount current withdrawal = " & sngAmtWithdrawal)
Msgbox( "
Total recent withdrawals = " & sngTotRecentWithdrawal)
Msgbox( "New Balance = " & sngBalance)

   sngAmtWithdrawal       sngBalance sng      TotRecentWithdrawal

1)       100                             575.50                                 0

Amount current withdrawal = 100
Total recent withdrawals = 100
New Balance = 475.50

2)       60                             150.00                                 450

Amount current withdrawal = 0
Total recent withdrawals = 450
New Balance = 150

3)       40                             1575.00                                 80

Amount current withdrawal = 40
Total recent withdrawals = 120
New Balance = 1535.00

4)       300                             10.00                                 100

Amount current withdrawal = 300 (explanation below)
Total recent withdrawals = 100
New Balance = 10

5)       100                             100.00                                 0

Amount current withdrawal = 100
Total recent withdrawals = 100
New Balance = 0

6)       250                             750.00                                 250

Amount current withdrawal = 0
Total recent withdrawals = 250
New Balance = 750



A problem with this code shows up in problem #4. Because there is no Else path for the first condition the output for the set of values in #4 is not what we would hope it to be. It is instead

Amount current withdrawal = 300
Total recent withdrawals = 100
New Balance = 10


The following selection structure is a corrected version.


If sngAmtWithdrawal <= sngBalance Then

    If (sngTotRecentWithdrawal + sngAmtWithdrawal) < 500 Then
       sngBalance = sngBalance - sngAmtWithdrawal
       sngTotRecentWithdrawal = sngTotRecentWithdrawal + sngAmtWithdrawal
   
Else
       sngAmtWithdrawal = 0
   
End If

Else

    sngAmtWithdrawal = 0
End If

Msgbox( "Amount current withdrawal = " & sngAmtWithdrawal)
Msgbox( "
Total recent withdrawals = " & sngTotRecentWithdrawal)
Msgbox( "
New Balance = " & sngBalance)



And will result in the following desired output

Amount current withdrawal = 0
Total recent withdrawals = 100
New Balance = 10