Working with
        Comparison
         Operators


Jesselle Capa

Charles Justine Bool
Comparison Operators
Operators that compare data values
  to produce true or false results.



            ASCII
 Contains a list of characters with
  corresponding unique numeric
         representation
Comparison Operators
Operator         Usage                           Description
   >       lblSales.Caption >   The greater than operator return True Only
           Goal                 if the value on the right.
   <       Pay < 2000.0         The less than operator return True if the
                                value on the left of < is less than the value
                                on the right.
   =       Age = Limit          The equal to operator return True if the
                                value on the both sides of = are equal.
  >=       FirstName >=         The greater than or equal to operator
           “Mike”               return True if the value on the left of >= is
                                greater than or equal to the value on the
                                right.
  <=       Num <=               The less than or equal to operator return
           lblAmt.Caption       True if the value on the left of <= is less
                                than or equal to the value on the right.
  <>       txtAns.text<>        The not equal to operator return True if the
           “Yes”                value on the left of <> is unequal to the
                                value on the right.
Relationship Results
        Relation             Result
          2>1                 True
          3<3                False
         5 > 10               True
   “Apple” <= “Orange”        True
“Macdonald “ < “Mc Donald”    True
          0 >=0               True
          0 <=0               True
          1 <>2               True
          2 >=3              False
Working with If Statement
   The If statement provides logic for the
    application, w/c analyzes data and makes
    decisions based on the analysis.

   The If statement uses comparison operators
    to test data values and performs one of two
    possible actions based on the results of the
    comparison‟s test.

   Without the If statement, the application
    code will sequentially execute . Meaning , one
    statement is executed after another.
The If statement is usually
written in the following
format:

     If comparisonTest Then

          One or more statements

     End If
If Statement and Comparison
   Keep in mind that the body of the If
    statement executes based on the results of
    the comparison test. The statement executes
    if the results is true. Otherwise , the rest of
    the application executes as usual and the If
    statement will be skipped.

   Data that are entered into a text box control
    is treated as a Variant data type . When
    arithmetic is performed with a Variant with
    data type that holds a numeric value . Visual
    Basic will convert such data to a number for
    the calculation purposes.
The If statement's Else Branching

   The If statement with Else is usually written in
    the following format:

            If comparisontest Then

                   one   or   more statements

            Else

                   one   or   more statements

            End If
Compound Comparison with the
         Logical Operators

Operator         Usage                  Description


  And      If (A > B) And (C <   Returns True if both sides
                    D)           of the And are true.


   Or      If (A > B) Or (C <    Returns True if either side
                   D)            of the Or is true.



  Not        If Not (strAns =    Returns the opposite true
                   “Yes”)        or false result.
Example :
Private Sub cmdGetStudentGrade_Click()

„Code for Generating Student's Grade
„Declare Variables
Dim percent As Integer, studentmark As String

„Clear text Box
   txtGrade.Text = ""

„Get Students Mark from inputbox
   percent = InputBox("Enter Students
  Percent")
'Begin if statement
   If percent >= 50 And percent < 60 Then
      studentmark = "C"
   Else percent >= 60 And percent < 70 Then
      studentmark = "B"
   Else percent >= 70 Then
      studentmark = "A"
   Else
      studentmark = "FAIL"
   End If

'Display Output
   txtGrade.Text = studentmark
End Sub
Nesting If-Else Statements

If (intAge = 5) Then
  lblTitle.Caption = “Kindergarten”
Else
  If (intAge = 6) Then
      lblTitle.Caption = “1st Grade”
  Else
      If (intAge = 7) Then
            lblTitle.Caption = “2nd Grade”
If (intAge = 8) Then
      lblTitle.Caption = “3rd Grade”
Else
     If (intAge = 9) Then
           lblTitle.Caption = “4th Grade”
     Else
          If (intAge = 10) Then
              lblTitle.Caption = “5th Grade”
          Else
               If (intAge = 11) Then
                  lblTitle.Caption = “6th Grade”
               Else
lblTitle.Caption   = "Advanced"
                 End If
             End If
          End If
       End If
     End If
  End If
End If
Using Select Case Statement
   The Select Case Statement handles multiple –
    choice conditions better than If-Else . When
    several choices are possible , programmers
    usually use Select Case as a substitute for
    long , nested If-Else , but you may find out
    that it is easier to code and to maintain .
    However , you must avoid using the Select
    Case if you will have a simple If or If-
    Else for the code , unless you need to
    compare against more than two values .
    Otherwise , stick with the simple If and If-
    Else statements
Select Case intAge
 Case 5 : lblTitle.Caption = "Kindergarten"
 Case 6 : lblTitle.Caption = "1st Grade"
 Case 7 : lblTitle.Caption = "2nd Grade"
 Case 8 : lblTitle.Caption = "3rd Grade"
 Case 9 : lblTitle.Caption = "4th Grade"
 Case 10 : lblTitle.Caption = "5th Grade
 Case 11 : lblTitle.Caption = "6th Grade
 Case Else : lblTitle.Caption = "Advanced"
End Select
3 Select Case Optional Formats:
   Select Case Expressions
    Case Is Relation :
       One or more Statements
     Case Is Relation :
       One or more Statements
    [Case Is Relation :
       One or more Statements]
    [Case Is Relation :
       One or more Statements]
   End Else
Select Case Expressions
 Case Value
     One or more Visual   Basic Statements
 Case Value
     One or more Visual   Basic Statements
 [Case Value
     One or more Visual   Basic Statements]
 [Case Value
     One or more Visual   Basic Statements]

End Else
Select Case Expressions

   Case expr1 To expr2 :
     One or more Visual Basic   Statements
   Case expr1 To expr2 :
     One or more Visual Basic   Statements
   [Case expr1 To expr2 :
     One or more Visual Basic   Statements]
   [Case Else :
     One or more Visual Basic   Statements]

End Else
Working with comparison operators
Working with comparison operators
Working with comparison operators
Working with comparison operators

Working with comparison operators

  • 1.
    Working with Comparison Operators Jesselle Capa Charles Justine Bool
  • 2.
    Comparison Operators Operators thatcompare data values to produce true or false results. ASCII Contains a list of characters with corresponding unique numeric representation
  • 3.
    Comparison Operators Operator Usage Description > lblSales.Caption > The greater than operator return True Only Goal if the value on the right. < Pay < 2000.0 The less than operator return True if the value on the left of < is less than the value on the right. = Age = Limit The equal to operator return True if the value on the both sides of = are equal. >= FirstName >= The greater than or equal to operator “Mike” return True if the value on the left of >= is greater than or equal to the value on the right. <= Num <= The less than or equal to operator return lblAmt.Caption True if the value on the left of <= is less than or equal to the value on the right. <> txtAns.text<> The not equal to operator return True if the “Yes” value on the left of <> is unequal to the value on the right.
  • 4.
    Relationship Results Relation Result 2>1 True 3<3 False 5 > 10 True “Apple” <= “Orange” True “Macdonald “ < “Mc Donald” True 0 >=0 True 0 <=0 True 1 <>2 True 2 >=3 False
  • 5.
    Working with IfStatement  The If statement provides logic for the application, w/c analyzes data and makes decisions based on the analysis.  The If statement uses comparison operators to test data values and performs one of two possible actions based on the results of the comparison‟s test.  Without the If statement, the application code will sequentially execute . Meaning , one statement is executed after another.
  • 6.
    The If statementis usually written in the following format: If comparisonTest Then One or more statements End If
  • 7.
    If Statement andComparison  Keep in mind that the body of the If statement executes based on the results of the comparison test. The statement executes if the results is true. Otherwise , the rest of the application executes as usual and the If statement will be skipped.  Data that are entered into a text box control is treated as a Variant data type . When arithmetic is performed with a Variant with data type that holds a numeric value . Visual Basic will convert such data to a number for the calculation purposes.
  • 8.
    The If statement'sElse Branching  The If statement with Else is usually written in the following format: If comparisontest Then one or more statements Else one or more statements End If
  • 9.
    Compound Comparison withthe Logical Operators Operator Usage Description And If (A > B) And (C < Returns True if both sides D) of the And are true. Or If (A > B) Or (C < Returns True if either side D) of the Or is true. Not If Not (strAns = Returns the opposite true “Yes”) or false result.
  • 10.
    Example : Private SubcmdGetStudentGrade_Click() „Code for Generating Student's Grade „Declare Variables Dim percent As Integer, studentmark As String „Clear text Box txtGrade.Text = "" „Get Students Mark from inputbox percent = InputBox("Enter Students Percent")
  • 11.
    'Begin if statement If percent >= 50 And percent < 60 Then studentmark = "C" Else percent >= 60 And percent < 70 Then studentmark = "B" Else percent >= 70 Then studentmark = "A" Else studentmark = "FAIL" End If 'Display Output txtGrade.Text = studentmark End Sub
  • 15.
    Nesting If-Else Statements If(intAge = 5) Then lblTitle.Caption = “Kindergarten” Else If (intAge = 6) Then lblTitle.Caption = “1st Grade” Else If (intAge = 7) Then lblTitle.Caption = “2nd Grade”
  • 16.
    If (intAge =8) Then lblTitle.Caption = “3rd Grade” Else If (intAge = 9) Then lblTitle.Caption = “4th Grade” Else If (intAge = 10) Then lblTitle.Caption = “5th Grade” Else If (intAge = 11) Then lblTitle.Caption = “6th Grade” Else
  • 17.
    lblTitle.Caption = "Advanced" End If End If End If End If End If End If End If
  • 18.
    Using Select CaseStatement  The Select Case Statement handles multiple – choice conditions better than If-Else . When several choices are possible , programmers usually use Select Case as a substitute for long , nested If-Else , but you may find out that it is easier to code and to maintain . However , you must avoid using the Select Case if you will have a simple If or If- Else for the code , unless you need to compare against more than two values . Otherwise , stick with the simple If and If- Else statements
  • 19.
    Select Case intAge Case 5 : lblTitle.Caption = "Kindergarten" Case 6 : lblTitle.Caption = "1st Grade" Case 7 : lblTitle.Caption = "2nd Grade" Case 8 : lblTitle.Caption = "3rd Grade" Case 9 : lblTitle.Caption = "4th Grade" Case 10 : lblTitle.Caption = "5th Grade Case 11 : lblTitle.Caption = "6th Grade Case Else : lblTitle.Caption = "Advanced" End Select
  • 20.
    3 Select CaseOptional Formats: Select Case Expressions Case Is Relation : One or more Statements Case Is Relation : One or more Statements [Case Is Relation : One or more Statements] [Case Is Relation : One or more Statements] End Else
  • 21.
    Select Case Expressions Case Value One or more Visual Basic Statements Case Value One or more Visual Basic Statements [Case Value One or more Visual Basic Statements] [Case Value One or more Visual Basic Statements] End Else
  • 22.
    Select Case Expressions Case expr1 To expr2 : One or more Visual Basic Statements Case expr1 To expr2 : One or more Visual Basic Statements [Case expr1 To expr2 : One or more Visual Basic Statements] [Case Else : One or more Visual Basic Statements] End Else