MIC 110

Selection Control Structures

Objectives:

·        Uses of simple selection, multiple selection and nested selection with If statements.

·        Case structure.

·        Contents:

 
[Simple Selection: If …Then]
[Simple selection with If..Then..Else]
[Extended selection with If..Then..ElseIf..]
[Nested If Statement]
[Comparison Operators]
[Logical Operators]
[Combined Conditions]
[The Case Structure]
[Case Keywords: Is, To & Else]
[Practice If Statement]

 

Simple Selection: If …Then

Simple selection executes its instruction(s) when the condition is met, depending on the result of a condition being true or false.  If the condition is false, then the entire If statement is skipped. The following is a common If statement structure in VB using If, Then and End If (with a space between End and If) key words.

If [condition] Then

            [instruction(s)]

End If

If sngTotalAmount > 500 Then

            sngShippingCharge = 0

End If

In this example, if the condition (sglTotalAmount > 500) is true, then the instruction (SglShippingCharge = 0) in the If statement is executed.  Otherwise, the If statement is skipped.

Although End If is not required for single instruction If statements, it is strongly recommended.

 

Simple selection with If..Then..Else

Simple selection If..Then..Else occurs when a choice is made between two alternative paths, depending on the result of a condition being true or false. 

If [condition] Then

            [instruction(s) 1]

Else

            [instruction(s) 2]

End If

If sglTotalAmount > 500 Then

      sglShippingCharge = 0

Else

      sglShippingCharge = 10

End If

If the condition (sglTotalAmount > 500) is true, instruction(s) 1 (sglShippingCharge = 0) is executed, otherwise (for all other possible cases) execute instruction(s) 2 (sglShippingCharge = 10) is exceuted.

Extended selection with If..Then..ElseIf..

If [condition 1] Then

      [instruction(s) 1]

ElseIf [condition 2] Then

      [instruction(s) 2]

ElseIf [condition 3] Then

      [instruction(s) 3]

End If

If sglTotalAmount > 500 Then

sglShippingCharge = 0

ElseIf sglTotalAmount > 300 Then

sglShippingCharge = 10

ElseIf sglTotalAmount > 0 Then

sglShippingCharge = 15

End If

If sglTotalAmount is greater than 500, then sglShippingCharge is 0, otherwise, compare if sglTotalAmount is greater than 300 (intrinsically, it isn't greater than 500).  If so, slgShippingCharge is 10.  Additional ElseIf clauses and statements can be added here.  If sglTotalAmount is greater 0 (and not greater than 300), then sglShippingCharge is 15.  Notice that once sglTotalAmount meets any condition, then the instruction(s) for the condision is executed and the If statement is terminated without going through the rest of the statement.  For example, slgTotalAmount is 600, then the sglShippingCharge becomes 0, and the rest of If statement is skipped.

Also, notice this is one If statement.

Nested If Statement

You may encounter a similar but different format from extended If statement.

If [condition 1] Then

      [instruction(s) 1]

Else

If [condition 2] Then

      [instruction(s) 2]

Else

      [instruction(s) 3]

End If

End If

If sglTotalAmount > 500 Then

sglShippingCharge = 0

Else

If sglTotalAmount > 300 Then

sglShippingCharge = 10

Else

sglShippingCharge = 15

End If

End If

The second If statement with condition 2 is a nested If statement of the outer If statement. So, the second If statement has its own control structure, and yet it get activated only and only if the Else statement of the outer If statement is activated. Here is an example program (Form | Project) that shows how nested If statement can solve a complex comparison problem.

Comparison Operators

Comparison Operator

Meaning

Example

=

Equal to

1 = 1 is true, 1=2 is false

<>

Not equal to

1 <> 1 is false, 1 <> 2 is true

>

Greater than

1 > 1 is false, 1 > 2 is false, 2 > 1 is true

<

Less than

1 < 1 is false, 1< 2 is true, 2 < 1 is false

>=

Greater than or equal to

1 >= 1 is true, 1 >= 2 is false, 2 >= 1 is true

<=

Less than or equal to

1 <= 1 is true, 1 <= 2 is true, 2 <= 1 is false

Logical Operators

You can combine more than one conditions using logical operators.

Logical Operator

Meaning

Example

And

Only if both conditions are true, then the result is true.

If [condition1] And [condition 2]

Or

If at least one condition is true, then the result is true

If [condition1] Or [condition 2]

Not

Negation of the condition.  If the condition is true, the result is false.  If the condition is false, then the result is true.

If Not [condition]

Xor

If one and only one of the conditions is true, then the result is true.  If all conditions are either true or false, then the result is false.  So, if all conditions are false, then the result is false.  If all conditions are true, then the result is also false. 

If blnStake Xor blnBurger Then

   strCustomer = “Satisfied”

End If

If a customer is served with either a stake or a burger, she is happy.  But if the customer is not served anything or both, then she is not happy. (too hungry or too full)

Combined Conditions

More than one condition can be tested in If clause. 

If (condition 1) Logical Operator (condition 2) Then

      (instruction)

End If

If (sglTotalAmount > 100) And (blnLoyalCustomer) Then

      sglShippingCharge = 0

End If

In the above example, blnLoyalCustomer is a Boolean data type which contains either True of False as data value. So, if blnLoyalCustomer = True had been executed, blnLoyalCustomer will have True as its value. Therefore, blnLoyalCustomer won't need a comparison operator.

 

The Case Structure

The case structure is another way of expressing a nested If statement. 

If [condition 1] Then

      [instruction 1]

ElseIf [(condition 2] Then

      [instruction 2]

ElseIf [condition 3] Then

      [instruction 3]

Else

      [instruction 4]

End If

Select Case [one variable]

     Case [value 1]

          [instruction 1]

     Case [value 2]

          [instruction 2]

     Case [value 3]

          [instruction 3]

     Case [value 4]

          [instruction 4]

End Select

Among all cases, only the first matching condition and its instruction is executed and the rest of the Case statement is terminated.

Sample:

Select Case intAge
       Case 16
              Label1.Caption = “You can drive now!”
       Case 18
              Label1.Caption = “You can vote now!”
       Case 21
              Label1.Caption = “You can drink wine!”
       Case 65
              Label1.Caption = “Time to retire and have fun!”
End Select

Equivalent:

If intAge = 16 Then
     Label1.Caption = “You can drive now!”
     ElseIf intAge = 18 Then
          Label1.Caption = “You can vote now!”
          ElseIf intAge = 21 Then
               Label1.Caption = “You can drink wine!”
               ElseIf intAge = 65 Then
                    Label1.Caption = “Time to retire and have fun!”
End If

 

Case Keywords: Is, To & Else

Dim intAge As Integer

Dim strMessage As String

intAge = Int(Val(txtAge.Text))

Select Case intAge

Case Is < 13

    strMessage = "Enjoy your youth!"

Case 13 To 19

    strMessage = "Enjoy your teens!"

Case 21

    strMessage = "You can drink wine with your meals!"

Case Is > 100

    strMessage = "Looking good!"

Case Else

    strMessage = "It's a nice age to be!"

End Select

txtMessage.Text = strMessage

Here are the file. (Form | Project)

 

Practice If Statement:

Part 1

    1. Let's start with Cel_To_Fah program. (Form Project)
    2. Add an If statement to display a few comments depending on the temperature. First, add a new text control (object) on the form. Let's name it txtMsg. Then add an If statement that will assign a text message if the tempF is greater than 80. The message could be "It's Hot!"
    3. Run the program. Use a test data set that will prove that the program works correctly - think abut many possible input data.
    4. Now, change the If statement to have two possible messages. One is "It's hot!" if tempF is greater than 80 and the other one is "It's not hot!" if tempF is less than or equal to 80.
    5. Run and test the program. Here are answer files: Form Project.
    6. Now modify the If statement to meet the following conditions. You should combine the conditions in one If statement.
    7. Here is one of possible answers. (Form Project)

     

Part 2

  1. Use the program from part 1, modify the If statement(s) to change the color of the messages depending the temperatures. E.g.:
  2. Hint: Chnage the forecolor of the txtComment control.
  3. Here is one of possible answers. (Form Project)

 

Part 3

  1. Modify the possible answers (Form Project) from Part 2.
  2. Instead of using one If statement, use 5 separate If statments.
  3. Some of If statements should use AND or OR operators.
  4. Here is one of possible answers. (Form Project)

Part 4

  1. Modify the possible answers (Form Project) from Part 3 to a Select Case structure.
  2. Here is one of possible answers. (Form Project)

Sample Program

[Form 1] [Form 2] [Project]