Multi-term Boolean expressions using Boolean operators

You can also use Boolean operators to create multi-term Boolean expressions. For example, this Boolean expression uses the and operator:

   strSex = "male" and intAge > 18
As you can guess, this expression would be true only for males greater than 18 years of age. This following expression uses the or operator, and it would be true for all men and for women over 21.
   strSex = "male" or intAge > 21
There are three Boolean operators in VB:

Boolean
Operator
Meaning
and both conditions must be true for the expression to be true
or if either conditions is true, the expression is true
not a true condition becomes false and a false one true

This program illustrates a multi-term Boolean expression using the or operator. It displays a pleasant greeting if the user is named "larry", "Larry", "laurence" or "Laurence"; otherwise, it displays a nasty greeting. Here is part of its listing:

Private Sub btnName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnName.Click
   Dim n As String
   n = txtName.Text
   If n = "larry" Or n = "Larry" Or n = "laurence" Or n = "Laurence" Then
      lblOut.Text = "You are kind of cute!"
   Else
      lblOut.Text = "You are kind of ugly."
   End If
End Sub
Private Sub txtName_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtName.Enter
   txtName.Clear()
   lblOut.Text = ""
End Sub

Do not be intimidated by this formal terminology. The bottom line is that the words "and," "or," and "not" are used in a common sense way. If you read Boolean expressions out loud, they make sense. Try reading this expression:

   strSex = not "male" and intAge > 21
This would be true for women (they are not males) who were over 21 years old.

Disclaimer: The views and opinions expressed on unofficial pages of California State University, Dominguez Hills faculty, staff or students are strictly those of the page authors. The content of these pages has not been reviewed or approved by California State University, Dominguez Hills.