Sie sind auf Seite 1von 10

2/13/2017 VisualBasicTutorialLesson6:LogicalComparisons

LogicalComparisons


LogicalOperations

Introduction
Sometimeswhileapersonisusingyourprogram,youmayneedtocheckwhethersomething
is true or it is false. This type of operation is performed using operators referred to as
comparisonoperators.VisualBasicprovidesvariousoperatorsthatcanbeusedinappropriate
typesofcomparisons.


BooleanVariables

The Boolean data type is used to declare a variable whose value would be set as true (1) or
false (0). To declare such a value, you use the Boolean keyword. The variable can then be
initialized with a starting value. The Boolean constant is used to check that the state of a
variable(orafunction)istrueorfalse.Youcandeclaresuchavariableas:

dimGotThePassingGradeasBoolean

Later in the program, for a student who got a failing grade, you can assign the other value,
likethis

GotThePassingGrade=False

LogicalOperators

Equality=
Tocomparetwovaluesforequality,usethe=operator.Itsformulais:

Value1=Value2

The equality operation is used to find out whether two variables (or one variable and a
constant) hold the same value. From our formula, the compiler would compare the value of
Value1 with that of Value2. If Value1 and Value2 hold the same value, the comparison
producesatrueresult.Iftheyaredifferent,thecomparisonrendersfalseor0.

http://www.functionx.com/vb6/Lesson06.htm 1/10
2/13/2017 VisualBasicTutorialLesson6:LogicalComparisons

Hereisanexample:

PrivateSubCommand1_Click()
DimvalueAsInteger

value=15
Text1.Text="ComparisonofValue=32produces"&(value=32)
EndSub

TheresultofacomparisoncanalsobeassignedtoaBooleanvariable.Hereisanexample:

PrivateSubCommand1_Click()
Dimvalue1AsInteger
Dimvalue2AsBoolean

value1=15

http://www.functionx.com/vb6/Lesson06.htm 2/10
2/13/2017 VisualBasicTutorialLesson6:LogicalComparisons

value2=(value1=32)
Text1.Text="ComparisonofValue=32produces"&value2
EndSub

LogicalNot
Whenavariableisdeclaredandreceivesavalue(thiscouldbedonethroughinitializationora
change of value) in a program, it becomes alive. It can then participate in any necessary
operation. The compiler keeps track of every variable that exists in the program being
processed. When a variable is not being used or is not available for processing (in visual
programming,itwouldbeconsideredasdisabled)tomakeavariable(temporarily)unusable,
youcannullifyitsvalue.Torenderavariableunavailableduringtheevolutionofaprogram,
applythelogicalnotoperatorwhichisNot.Itsformulais:

NotValue

There are two main ways you can use the logical Not operator. As we will learn when
studying conditional statements, the most classic way of using the logical Not operator is to
checkthestateofavariable.

To nullify a variable, you can write Not to its left. When used like that, you can display its
value.Youcanevenassignittoanothervariable.Hereisanexample:

PrivateSubCommand1_Click()
Dimvalue1AsInteger
Dimvalue2AsBoolean

value1=250
value2=Notvalue1

Text1.Text=value2
EndSub

Whenavariableholdsavalue,itis"alive".Tomakeitnotavailable,youcan"not"it.Whena
variable has been "notted", its logical value has changed. Therefore, you can inverse the
logicalvalueofavariableby"notting"ornot"notting"it.ThisisdonebytypingNottoitsleft.

Inequality<>
VisualBasicprovidesanoperatorusedtocomparetwovaluesforinequality.Itsformulais:

Value1<>Value2

http://www.functionx.com/vb6/Lesson06.htm 3/10
2/13/2017 VisualBasicTutorialLesson6:LogicalComparisons

<> is a binary operator (like all logical operators except the logical Not, which is a unary
operator)thatisusedtocomparetwovalues.Thevaluescancomefromtwovariablesasin
Variable1<> Variable2. Upon comparing the values, if both variables hold different values,
thecomparisonproducesatrueorpositivevalue.Otherwise,thecomparisonrendersfalseor
anullvalue.

Hereisanexample:

PrivateSubCommand1_Click()
Dimvalue1AsInteger
Dimvalue2AsInteger
Dimvalue3AsBoolean

value1=212
value2=46
value3=(value1<>value2)

Text1.Text=value1&"<>"&value2&"="&value3
EndSub

http://www.functionx.com/vb6/Lesson06.htm 4/10
2/13/2017 VisualBasicTutorialLesson6:LogicalComparisons

Theinequalityisobviouslytheoppositeoftheequality.

LessThan<
Tofindoutwhetheronevalueislowerthananother,usethe<operator.Itsformulais:

Value1<Value2

The value held by Value1 is compared to that of Value2. As it would be done with other
operations,thecomparisoncanbemadebetweentwovariables,asinVariable1<Variable2.
IfthevalueheldbyVariable1islowerthanthatofVariable2,thecomparisonproducesatrue
orpositiveresult.

Hereisanexample:

PrivateSubCommand1_Click()
http://www.functionx.com/vb6/Lesson06.htm 5/10
2/13/2017 VisualBasicTutorialLesson6:LogicalComparisons
PrivateSubCommand1_Click()
Dimvalue1AsInteger
Dimvalue2AsInteger
Dimvalue3AsBoolean

value1=212
value2=46
value3=(value1<value2)

Text1.Text=value1&"<"&value2&"="&value3
EndSub

LessThanOrEqual<=
Theprevioustwooperationscanbecombinedtocomparetwovalues.Thisallowsyoutoknow
iftwovaluesarethesameorifthefirstislessthanthesecond.Theoperatorusedis<=and
itsformulais:

Value1<=Value2

The <= operation performs a comparison as any of the last two. If both Value1 and Value2
hold the same value, the result is true or not null. If the left operand, in this case Value1,
holdsavaluelowerthanthesecondoperand,inthiscaseValue2,theresultisstilltrue.

http://www.functionx.com/vb6/Lesson06.htm 6/10
2/13/2017 VisualBasicTutorialLesson6:LogicalComparisons

Hereisanexample:

PrivateSubCommand1_Click()
Dimvalue1AsInteger
Dimvalue2AsInteger
Dimvalue3AsBoolean

value1=212
value2=46
value3=(value1<=value2)

Text1.Text=value1&"<="&value2&"="&value3
EndSub

GreaterThan>
Whentwovaluesofthesametypearedistinct,oneofthemisusuallyhigherthantheother.
Visual Basic provides a logical operator that allows you to find out if one of two values is
greaterthantheother.Theoperatorusedforthisoperationusesthe>symbol.Itsformulais:

Value1>Value2

Bothoperands,inthiscaseValue1andValue2,canbevariablesortheleftoperandcanbea
variable while the right operand is a constant. If the value on the left of the > operator is
greater than the value on the right side or a constant, the comparison produces a true or
positive value. Otherwise, the comparison renders false or null. This can be illustrated as
follows:

http://www.functionx.com/vb6/Lesson06.htm 7/10
2/13/2017 VisualBasicTutorialLesson6:LogicalComparisons

Hereisanexample:

PrivateSubCommand1_Click()
Dimvalue1AsInteger
Dimvalue2AsInteger
Dimvalue3AsBoolean

value1=212
value2=46
value3=(value1>value2)

Text1.Text=value1&">"&value2&"="&value3
EndSub

GreaterThanorEqual>=
Thegreaterthanortheequalityoperatorscanbecombinedtoproduceanoperatorasfollows:
>=.Thisisthe"greaterthanorequalto"operator.Itsformulais:

Value1>=Value2

http://www.functionx.com/vb6/Lesson06.htm 8/10
2/13/2017 VisualBasicTutorialLesson6:LogicalComparisons

A comparison is performed on both operands: Value1 and Value2. If the value of Value1 and
thatofValue2arethesame,thecomparisonproducesatrueorpositivevalue.Ifthevalueof
the left operand is greater than that of the right operand, the comparison produces true or
positive also. If the value of the left operand is strictly less than the value of the right
operand,thecomparisonproducesafalseornullresult.

Thiscanbeillustratedasfollows:

Hereisanexample:

PrivateSubCommand1_Click()
Dimvalue1AsInteger
Dimvalue2AsInteger
Dimvalue3AsBoolean

value1=212
value2=46
value3=(value1>=value2)

Text1.Text=value1&">="&value2&"produces"&value3
EndSub

http://www.functionx.com/vb6/Lesson06.htm 9/10
2/13/2017 VisualBasicTutorialLesson6:LogicalComparisons

Hereisasummarytableofthelogicaloperatorswehavestudied:

Operator Meaning Example Opposite


= Equalityto a=b <>
<> Notequalto 12<>7 =
< Lessthan 25<84 >=
<= Lessthanorequalto Cab<=Tab >
> Greaterthan 248>55 <=
Greaterthanorequal
>= Val1>=Val2 <
to

Previous Copyright20052015,FunctionX Next

http://www.functionx.com/vb6/Lesson06.htm 10/10

Das könnte Ihnen auch gefallen