Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

Delphi Programming/Boolean expression

From Wikibooks, open books for an open world
<Delphi Programming
Thelatest reviewed version waschecked on1 March 2019. There are2 pending changes awaiting review.

A boolean expression returns a boolean value that can be used directly or stored in a boolean variable. An expression is created on a operator (and, or, xor, <, >...) which needs a given number of operands which can be a hard coded value, a variable or even another expression. A boolean expression can use boolean or numerical values:

programGo_to_the_playground;{$APPTYPE CONSOLE}usesSysUtils;varage:integer;beginWriteLn('How old are you?');ReadLn(age);Write('Allowed to play: ');WriteLn((age>3)and(age<12));// Display "Allowed to play: True" or "Allowed to play: False"end.

Logical operators

[edit |edit source]

The logical operators allow to handle and compare the boolean or binary data. If you are not familiar with this concept, learn more about theboolean algebra.

Operator "not"

[edit |edit source]

The NOT logical operator is writtennot in Delphi. So to get the opposite of a value in Delphi, we write:

varx,y:boolean;beginx:=true;y:=notx;// y = falseend.

Operator "and"

[edit |edit source]

The AND logical operator is writtenand in Delphi. It's like& in C. So to make a bitwise and on values in Delphi, we write:

varx,y,z:boolean;beginx:=true;y:=false;z:=yandx;// z = falseend.

Operator "or"

[edit |edit source]

The OR logical operator is writtenor in Delphi. It's like| in C. So to make an or on values in Delphi, we write:

varx,y,z:boolean;beginx:=true;y:=false;z:=yorx;// z = trueend.

Operator "xor" (symmetric difference, or exclusive)

[edit |edit source]

The XOR logical operator is writtenxor in Delphi. So to make an xor on values in Delphi, we write:

varx,y,z:boolean;beginx:=true;y:=false;z:=yxorx;// z = trueend.

Comparison operators

[edit |edit source]

They are mostly used to do logical tests on numerical values.

Equality "="

[edit |edit source]

The Delphi equality comparator '= allows to verify that the value on the left is strictly equal to the value on the right.

It is the same as do an inversion on the XOR logical operator (i.e. NOT XOR). Here is the truth table of the = comparator for the (x = y) operation:

= (NOT XOR)y = truey = false
x = truetruefalse
x = falsefalsetrue

Difference "<>"

[edit |edit source]

The Delphi differential operator <> allows to verify that the value on the left is strictly different to the value on the right:

varx,y:Integer;isDifferent:boolean;beginx:=5;y:=10;isDifferent:=(x<>y);// isDifferent = trueend.

Comparator "<" (strictly less than) :

[edit |edit source]

The comparator "<" (strictly less than) allows to verify that the value on the left is strictly less than to the value on the right. This operator returns true only if the numerical value is strictly less than to the numerical value on the right.

Comparator "<=" (less than) :

[edit |edit source]

The comparator "<=" (less than) allows to verify that the value on the left is less than or equal to, to the value on the right. This operator returns true only if the numerical value is less than or equal to, the numerical value on the right.

Comparator ">=" (greater than) :

[edit |edit source]

The comparator ">=" (greater than) allows to verify that the value on the left is greater than or equal to, the value on the right. This operator returns true only if the numerical value is greater than or equal to the numerical value on the right.

Comparator ">" (strictly greater than) :

[edit |edit source]

The comparator ">" (strictly greater than) allows to verify that the value on the left is strictly greater than the value on the right. This operator returns true only if the numerical value is strictly greater than the numerical value on the right.

Expression combination

[edit |edit source]

You can create a boolean expression on boolean expressions instead of variables:

varx,y,z:boolean;areAllEqual:boolean;beginx:=false;y:=false;z:=true;areAllEqual:=((x=y)and(x=z)and(y=z));// areAllEqual = falseend.

A boolean expression on boolean expressions will use the returned values of the nested expressions. The nested expressions are in the brackets. They are evaluated first. In other word, if you change the brackets, the expressions will be evaluated differently:

varx,y,z:boolean;areAllEqual:boolean;beginx:=false;y:=false;z:=true;areAllEqual:=((x=y)and((x=(zandy))=z));// areAllEqual = trueend.

So beware of the order.

Retrieved from "https://en.wikibooks.org/w/index.php?title=Delphi_Programming/Boolean_expression&oldid=4533325"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp