IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
   ► KBTo/From GuidesReferenceOperators  Print This     

Logical Operators (Cross Ref > Operators)

Logical Operators

Languages Focus

Logical operators perform conditional and, or, and not operations. Some languages support both binary logical operators that link two and unary logical operators negate (make opposite) the truth value of its argument. Finally, some languages short circuit logic. For example, with this or that, if this is an expression returning true, then that is never executed.

Access VBA:   and, or, not

Same as VB. Access VBA logical operators:

and and, as in this and that
or or, as in this or that
Not Not, as in Not This

Access VBA never short circuits. Given the expression this or that as well as this and that, if this evaluates to false, then that is still executed.

Syntax Example:
'Given expressions a, b, c, and d:
If Not (a and b) and (c or d) Then
  'Do something.
End If

Using the Logical Not Operator

You can use the not operator in many contexts. One of my favorite uses for it is to toggle boolean properities with a single line of code:

BooleanProperty = Not BooleanProperty

MS Access 2003 Working Demo

The following demo uses the not operator to toggle the visible property of a Image object from a button.

  1. Create a new form and place a button and a Image on it. Add a picture to the Image with the Picture property.
      
    Demo of using not operator.
     
  2. Edit the Click event of the button and alter it as follows below. Select the button and edit the On Click event on the Event tab of the Properities explorer. Your Image object should be named Image1, but if it's not, use the correct object name.
     
    Private Sub Command1_Click()
        Image1.Visible = Not Image1.Visible
    End Sub

     
  3. Run the form and test. Select View | Form View and click the button. You'll notice the picture toggles between visible and not visible with a single line of code.

Access VBA Short Circuting Example:

In the following example, if Access VBA supported short circuting, the That function would never execute.

Function This()
    MsgBox ("The This function executed.")
    This = False
End Function
 
Function That()
    MsgBox ("The That function executed!")
    That = True
End Function
 
Private Sub Command0_Click()
    'Notice both the This and That functions execute
    'even though the This Function returned False.
    If This And That Then
        MsgBox ("hi")
    End If
End Sub


Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

Beginner

1 Beginner Level Question

Question #1: True or False?

Given the following code, which MsgBox will display? The True or the False one?

Dim a, b, c, d As Boolean
  
a = True
b = True
c = True
d = False
 
If Not (a And b) And (c Or d) Then
  MsgBox ("True")
Else
  MsgBox ("False")
End If
Answer:
  • True
  • False
  • Intermediate

    1 Intermediate Level Question

    Question #2: Yes or No?

    Given the following two functions:

    Function This()
        This = False
    End Function
     
    Function That()
        That = True
    End Function

     

    Will the That function in the following code execute?

    Private Sub Command0_Click()
        If This And That Then
            MsgBox ("hi")
        End If
    End Sub
    Answer:
  • Yes
  • No
  • More Info

    Code:  Access VBA Logical Operators (and, or, not)

    ASP Classic:   and, or, not

    Same as VB. ASP Classic logical operators:

    and and, as in this and that
    or or, as in this or that
    Not Not, as in Not This

    ASP Classic never short circuits. Given the expression this or that as well as this and that, if this evaluates to false, then that is still executed.

    Syntax Example:
    'Given expressions a, b, c, and d:
    If Not (a and b) and (c or d) Then
      'Do something.
    End If

    ASP Classic Short Circuting Example:

    In the following example, if ASP Classic supported short circuting, the That function would never execute. It's interesting to note that VB.Net has introduced two new operators to support short cicuiting: AndAlso and OrElse for use when you code in ASP.Net.

    <%@LANGUAGE='VbScript'%>
    <%Option Explicit%>
    <html>
    <body>
    <h1>Short Circuting</h1>
    <%
    Function This()
        Response.Write("<br>The This function executed.")
        This = False
    End Function
     
    Function That()
        Response.Write("<br>The That function executed!")
        That = True
    End Function
     
    'Notice both the This and That functions execute
    'even though the This Function returned False.
    If This And That Then
      Response.Write("<br>Both are true.")
    End If

    %>
    </body>
    </html>


    Linked Certification Question(s)

    The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

    Beginner

    1 Beginner Level Question

    Question #1: True or False?

    Given the following code, will True or False be written?

    Dim a, b, c, d
     
    a = True
    b = True
    c = True
    d = False 
     
    If Not (a And b) And (c Or d) Then
      Response.Write("True")
    Else
      Response.Write("False")
    End If
    Answer:
  • True
  • False
  • Intermediate

    1 Intermediate Level Question

    Question #2: Yes or No?

    Given the following two functions:

    Function This()
        This = False
    End Function
     
    Function That()
        That = True
    End Function

     

    Will the That function in the following code execute?

    If This And That Then
      Response.Write("<br>hello.")
    End If
    Answer:
  • Yes
  • No
  • More Info

    Code:  ASP Classic Logical Operators (and, or, not)

    C#: 

    Same as C++ and Java. C# logical operators:

    & and, as in this and that No Short Circuit
    && and, as in this and that short circuits
    | or, as in this or that No Short Circuit
    || or, as in this or that short circuits
    ! Not, as in Not This
    ^ either or, as in this or that but not both

    Syntax Example:
    //Given expressions a, b, c, and d:
    if !((a && b) && (c || d)) 
    {
      //Do something.
    }

    More Info

    C++: 

    C++ logical operators:

    && and, as in this and that
    || or, as in this or that
    ! Not, as in Not This
    ^ either or, as in this or that but not both

    Syntax Example:
    //Given expressions a, b, c, and d:
    if !((a && b) && (c || d)) {
      //Do something.
    }

    More Info

    C++/CLI: 

    Same as C++ and Java. C# logical operators:

    & and, as in this and that No Short Circuit
    && and, as in this and that short circuits
    | or, as in this or that No Short Circuit
    || or, as in this or that short circuits
    ! Not, as in Not This
    ^ either or, as in this or that but not both

    Syntax Example:
    //Given expressions a, b, c, and d:
    if !((a && b) && (c || d)) {
      //Do something.
    }

    More Info

    Corel Paradox: 

    ObjectPAL logical operators:

    and and, as in this and that
    or or, as in this or that
    Not Not, as in Not This

    Like VBA, ObjectPAL never short circuits. Given the expression this or that as well as this and that, if this evaluates to false, then that is still executed.

    Syntax Example:
    ;Given expressions a, b, c, and d:
    if Not (a and b) and (c or d) then
      ;Do something.
    endIf

    Simple Short Circuit Code Test

    The following code snippet allows you to test out ObjectPAL's short circuiting. In the if statement of the pushButton event, the That() custom procedure executes even if This() is False.

    To use the code, alter a pushButton event of a button as follows:

    ;This custom procedure.
    proc This() Logical
      msgInfo("", "This")
       return False
    endProc
     
    ;That custom procedure.
    proc That() Logical
      msgInfo("", "That")
       return True
    endProc
     
    method pushButton(var eventInfo Event)
       ;Object always short circuits.
       ;The That() custom procedure executes even 
       ;though This() returns false.
       if This() and That() then
         msgInfo("", "both are true")
       endIf
    endMethod

    Using the Logical Not Operator

    You can use the not operator in many contexts. One of my favorite uses for it is to toggle boolean properities with a single line of code:

    BooleanProperty = Not BooleanProperty

    Working Paradox Demo

    The following demo works in all versions of Paradox for Windows. The demo uses the not operator to toggle the visible property of a box from a button.

    1. Create a new form and place a button and a box on it.
       
      Paradox form.
       
    2. Name the box "TheBox". To rename it, select the box, right click and click properties. In the Name of object field, change the name to MyBox.
       
      Demo of using not operator.
        
    3. Edit the pushButton event and alter it as follows below. Select the button, right click, click Object Explorer. Double click the pushButton event on the Events tab.
       
      method pushButton(var eventInfo Event)
        TheBox.Visible = Not TheBox.Visible
      endMethod

        
    4. Run the form and test. Select Program | Run and click the button. You'll notice the visible box toggles between visible and not visible with a single line of code.
       


    Linked Certification Question(s)

    The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

    Beginner

    1 Beginner Level Question

    Question #1: Multiple Choice

    What are the logical operators?

    Answer:
    1. 
    • and
    • or
    • not
    • xor
    2. 
    • and
    • or
    • not
    3. 
    • and
    • andAlso
    • or
    • orElse
    • not
    4. 
    • and
    • andAlso
    • or
    • orElse
    • not
    • xor

    More Info

    Code:  ObjectPAL Logical Operators

    Delphi: 

    Delphi logical operators:

    and and, as in this and that
    or or, as in this or that
    not Not, as in Not This
    xor either or, as in this or that but not both

    The Delphi compiler default is to short circuit multi argument boolean expressions when the result is known before the evaluation completes. To disable short circuiting, use the {$B+} compiler directive. To reset it back to the compiler default of short circuting, use the {$B-} compiler directive.

    Syntax Example:
    //Given expressions a, b, c, and d:
    if Not (a and b) and (c or d) then
      //Do something.

    Using the Logical Not Operator

    You can use the not operator in many contexts. One of my favorite uses for it is to toggle boolean properities with a single line of code:

    BooleanProperty = Not BooleanProperty;

    For example, if you have an Image control on a form you can toggle it's visibility with a single line of code:

    Image1.Visible := Not Image1.Visible;

     



    Linked Certification Question(s)

    The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

    Beginner

    1 Beginner Level Question

    Question #1: True or False?

    Given the following code, which ShowMessage will display? The True or the False one?

    var
      a, b, c, d: Boolean;
    begin
      a := True;
      b := True;
      c := True;
      d := True;
      
      if Not (a and b) and (c or d) then
        ShowMessage('True')
      else
        ShowMessage('False');
    end;
    Answer:
  • True
  • False
  • More Info

    Code:  Delphi Logical Operators

    Delphi Prism: 

    Prism logical operators:

    and and, as in this and that
    or or, as in this or that
    not Not, as in Not This
    xor either or, as in this or that but not both

    Syntax Example:
    //Given expressions a, b, c, and d:
    if Not (a and b) and (c or d) then
      //Do something.


    Linked Certification Question(s)

    The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

    Beginner

    1 Beginner Level Question

    Question #1: True or False?

     Given the following code, which MessageBox.Show will display? The True or the False one?

    var a, b, c, d: Boolean;
      
    a := False;
    b := False;
    c := True;
    d := False;

    if Not (a and b) and (c or d) then
    MessageBox.Show('True')
    else
    MessageBox.Show('False');

     

    Answer:
  • True
  • False
  • More Info

     

    Java: 

    Java logical operators:

    && and, as in this and that
    || or, as in this or that
    ! Not, as in Not This
    & boolean logical OR (not short circuited)
    | boolean logical OR (not short circuited)
    ?: Ternary (short for if-then-else)
    ~ Unary bitwise complement
    << Signed left shift
    >> Signed right shift
    >>> Unsigned right shift
    ^ Bitwise exclusiv OR

    Syntax Example:
    //Given expressions a, b, c, and d:
    if !((a && b) && (c || d)) {
      //Do something.
    }

    More Info

    JavaScript: 

    JavaScript logical operators:

    && and, as in this and that
    || or, as in this or that
    ! Not, as in Not This

    JavaScript always short circuits. Given the expression this || that, if this evaluates to true, then that is never executed.

    Syntax Example:
    //Given expressions a, b, c, and d:
    if !((a && b) && (c || d)) {
      //Do something.
    }

    More Info

    Perl: 

    Perl logical operators:

    && or and and, as in this and that
    || or or or, as in this or that
    ! Not, as in Not This

    Syntax Example:
    #Given expressions a, b, c, and d:
    if !((a && b) && (c || d)) {
      #Do something.
    }

    More Info

    PHP:   and, &&, or, ||, !, Xor

    PHP logical operators:

    and, && and, as in this and that
    or, || or, as in this or that
    ! Not, as in Not This
    Xor either or, as in this or that but not both

    Syntax Example:
    #Given expressions a, b, c, and d:
    if !((a && b) && (c || d)) {
      #Do something.
    };

    More Info

    VB Classic:   and, or, not

    VB Classic logical operators:

    and and, as in this and that
    or or, as in this or that
    Not Not, as in Not This

    VB Classic never short circuits. Given the expression this or that as well as this and that, if this evaluates to false, then that is still executed.

    Syntax Example:
    'Given expressions a, b, c, and d:
    If Not (a and b) and (c or d) Then
      'Do something.
    End If

    Using the Logical Not Operator

    You can use the not operator in many contexts. One of my favorite uses for it is to toggle boolean properities with a single line of code:

    BooleanProperty = Not BooleanProperty

    VB6 Working Demo

    The following demo uses the not operator to toggle the visible property of a PictureBox from a button.

    1. Create a new form and place a button and a PictureBox on it. Add a picture to the PictureBox with the Picture property.
       
      Demo of using not operator.
       
    2. Edit the Click event and alter it as follows below (double click the button). Your PictureBox should be named Picture1, but if it's not, use the correct object name.
       
      Private Sub Command1_Click()
          Picture1.Visible = Not Picture1.Visible
      End Sub

       
    3. Run the application and test. Select Run| Start and click the button. You'll notice the picture toggles between visible and not visible with a single line of code.

    VB Classic Short Circuting Example:

    In the following example, if VB Classic supported short circuting, the That function would never execute. It's interesting to note that VB.Net has introduced two new operators to support short cicuiting: AndAlso and OrElse but you'll have to move to VB.Net to take advantage of them.

    Function This()
        MsgBox ("The This function executed.")
        This = False
    End Function
     
    Function That()
        MsgBox ("The That function executed!")
        That = True
    End Function
     
    Private Sub Command0_Click()
        'Notice both the This and That functions execute
        'even though the This Function returned False.
        If This And That Then
            MsgBox ("hi")
        End If
    End Sub


    Linked Certification Question(s)

    The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

    Beginner

    1 Beginner Level Question

    Question #1: True or False?

    Given the following code, which MsgBox will display? The True or the False one?

    Dim a, b, c, d As Boolean
      
    a = True
    b = True
    c = True
    d = False
     
    If Not (a And b) And (c Or d) Then
      MsgBox ("True")
    Else
      MsgBox ("False")
    End If
    Answer:
  • True
  • False
  • Intermediate

    1 Intermediate Level Question

    Question #2: Yes or No?

    Given the following two functions:

    Function This()
        This = False
    End Function
     
    Function That()
        That = True
    End Function

     

    Will the That function in the following code execute?

    Private Sub Command0_Click()
        If This And That Then
            MsgBox ("hi")
        End If
    End Sub
    Answer:
  • Yes
  • No
  • More Info

    Code:  VB Classic Logical Operators (and, or, not)

    VB.Net: 

    VB.Net logical operators:

    And and, as in this and that No Short Circuit
    AndAlso and, as in this and that short circuits
    Or or, as in this or that No Short Circuit
    OrElse or, as in this or that short circuits
    Not Not, as in Not This
    Xor either or, as in this or that but not both

    Syntax Example:
    'Given expressions a, b, c, and d:
    If Not (a and b) and (c or d) Then
      'Do something.
    End If


    Linked Certification Question(s)

    The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

    Beginner

    1 Beginner Level Question

    Question #1: Multiple Choice

    What are the logical operators?

    Answer:
    1. 
    • And
    • Or
    • Not
    2. 
    • &
    • &&
    • |
    • ||
    • !
    • ^
    3. 
    • And
    • AndAlso
    • Or
    • OrElse
    • Not
    • XOR
    4. 
    • And
    • Or
    • Not
    • XOR
    5. 
    • &
    • |
    • !
    • ^

    More Info

     




    Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
    www.prestwood.com


    ©1995-2024 Prestwood IT Solutions.   [Security & Privacy]