Nested If

 

There are times when we want to do something only when two separate conditions are true. For this we can use a Nested If statement.

 

If condition Then

If condition Then

statement

statement

End if

End if

 

q       The second If statement will only be checked when the first condition is true that is the second If statement begins the block of statements that will be executed only when the first If statement is True

q       Indentation of subordinate statements is not syntactically required but is required for good programming technique.

q       Each If MUST be paired with an End If

 

For example

 

                blnHonorStudent = False

The second test      (intHoursCompleted >= 15)

will only take place if intGPA > 3.5

 

blnHonorStudent will be assigned the value of True only if both conditions are true.

 
 


If intGPA > 3.5 then

        If intHoursCompleted >= 15 then

blnHonorStudent = true

End if

                End if

 

                lblHonor.Text = blnHonorStudent

 

 

The second test      (intYearsEmployed > 2)

will only take place if sngSales IS greater than 300000

 

sngBonus will be assigned the value of 1000 only if both conditions are true

 

If  sngSales is greater than 300000 BUT intYearsEmployed is NOT greater than 2 sngBonus will be made equal to 500.

 

Notice if sngSales is NOT greater than 300000 no value is placed in sngBonus.

 

 

 
                sngBonus = 0

 

If sngSales > 300000 Then

                        If intYearsEmployed > 2 Then

                                sngBonus = 1000

                        Else

                                sngBonus = 500

                        End If

End If

 

                MsgBox (" Bonus = " & sngBonus)

If intAge is greater than 17 AND blnRegistered = True the message will be “You may vote”.

If intAge is greater than 17 BUT blnRegistered is not True the message “You must register to vote “ will be displayed.

 
 


  

If intAge > 17 Then

        If blnRegistered Then

                lblMessage.Text= "You may vote”

        Else

                lblmessage.Text= “You must register to vote”

If intAge is NOT greater than 17 the variable blnRegistered will not be tested. The message “You are too young to vote” will be displayed.

 
        End If

Else

        lblmessage.Text= “You are too young to vote”

End If

 

 

If sngAmtWithdrawal <= sngBalance Then

        If sngAmtRecentWithdrawal + sngAmtWithdrawal <= 500 then

                sngBalance = sngBalance -  sngAmtWithdrawal

                sngAmtRecentWithdrawal = sngAmtRecentWithdrawal + sngAmtWithdrawal

If  sngAmtWithdrawal <= sngBalance the program will then test to see if the sum of sngAmtRecentWithdrawal and sngAmtWithdrawal is less than or equal to 500 (imagine a daily withdrawal limit of $500).

If BOTH conditions are true a new sngBbalance and sngAmtWithdrawal will be calculated.

If sngAmtWithdrawal <= sngBalance BUT the withdrawal combined with recent withdrawals will exceed the limit the withdrawal is denied.

If there is not enough money to cover the withdrawal the withdrawal is denied, the test for exceeding the limit is not checked.

 
Else

        sngAmtWithdrawal = 0

End If

Else

        sngAmtWithdrawal = 0

End If

 

lblWithdrawal.Text = "Amount current withdrawal = " & sngAmtWithdrawal

lblRecentWith.Text = "Total recent withdrawals = " & sngAmtRecentWithdrawal

lblNewBalance = "New Balance = " & sngBalance