Jeremy Collins said:
Where did you learn programming?
Behind my computer =D
Fortunately turbo pascal and delphi are great programming languages to learn
to program.
Delphi's help comes to the rescue once again and teaches me how booleans
work:
"
The four predefined Boolean types are Boolean, ByteBool, WordBool, and
LongBool. Boolean is the preferred type. The others exist to provide
compatibility with other languages and operating system libraries.
A Boolean variable occupies one byte of memory, a ByteBool variable also
occupies one byte, a WordBool variable occupies two bytes (one word), and a
LongBool variable occupies four bytes (two words).
Boolean values are denoted by the predefined constants True and False. The
following relationships hold.
Boolean ByteBool, WordBool, LongBool
False < True False <> True
Ord(False) = 0 Ord(False) = 0
Ord(True) = 1 Ord(True) <> 0
Succ(False) = True Succ(False) = True
Pred(True) = False Pred(False) = True
A value of type ByteBool, LongBool, or WordBool is considered True when its
ordinality is nonzero. If such a value appears in a context where a Boolean
is expected, the compiler automatically converts any value of nonzero
ordinality to True.
The previous remarks refer to the ordinality of Boolean values, not to the
values themselves. In Delphi, Boolean expressions cannot be equated with
integers or reals. Hence, if X is an integer variable, the statement
if X then ...;
generates a compilation error. Casting the variable to a Boolean type is
unreliable, but each of the following alternatives will work.
if X <> 0 then ...; { use longer expression that returns Boolean
value }
var OK: Boolean { use Boolean variable }
...
if X <> 0 then OK := True;
if OK then ...;
"
What do they mean with ordinality ?
More text from help files:
"
Ordinal types include integer, character, Boolean, enumerated, and subrange
types. An ordinal type defines an ordered set of values in which each value
except the first has a unique predecessor and each value except the last has
a unique successor. Further, each value has an ordinality which determines
the ordering of the type. In most cases, if a value has ordinality n, its
predecessor has ordinality n - 1 and its successor has ordinality n + 1.
For integer types, the ordinality of a value is the value itself.
Subrange types maintain the ordinalities of their base types.
For other ordinal types, by default the first value has ordinality 0, the
next value has ordinality 1, and so forth. The declaration of an enumerated
type can explicitly override this default.
Several predefined functions operate on ordinal values and type identifiers.
The most important of them are summarized below.
Function Parameter Return value Remarks
Ord ordinal expression ordinality of expression's value Does not take Int64
arguments.
Pred ordinal expression predecessor of expression's value
Succ ordinal expression successor of expression's value
High ordinal type identifier or variable of ordinal type highest value in
type Also operates on short-string types and arrays.
Low ordinal type identifier or variable of ordinal type lowest value in type
Also operates on short-string types and arrays.
For example, High(Byte) returns 255 because the highest value of type Byte
is 255, and Succ(2) returns 3 because 3 is the successor of 2.
The standard procedures Inc and Dec increment and decrement the value of an
ordinal variable. For example, Inc(I) is equivalent to I := Succ(I) and, if
I is an integer variable, to I := I + 1.
"
The order of values ?
Well how I am supposed to know the order of values ?
I can imagine the compiler defining integer values as:
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 0
or
0 1 2 3 4 5 6 7 8 9 -1 -2 -3 -4 -5 -6 -7 -8 -9
Etc.
Though this is documented above:
"For integer types, the ordinality of a value is the value itself."
What about booleans... well these little functions give it away:
Ord(False) = 0
Ord(True) = 1
Now back to this statement:
"
A value of type ByteBool, LongBool, or WordBool is considered True when its
ordinality is nonzero. If such a value appears in a context where a Boolean
is expected, the compiler automatically converts any value of nonzero
ordinality to True.
The previous remarks refer to the ordinality of Boolean values, not to the
values themselves. In Delphi, Boolean expressions cannot be equated with
integers or reals. Hence, if X is an integer variable, the statement
"
Especially this part:
"the compiler automatically converts any value of nonzero ordinality to
True"
What is therefore the ordinality of -1 ?
Good question eh ?
Apperently ordinality can be negative as well.
Well at least now I understand how delphi defines booleans and false and
true etc.
However I am not a C programmer and I don't care that much about C... it's
just bitching when one has to read C code
I have done my part and explained how Delphi works.
Are you good enough to explain how C works ?
Bye,
Skybuck.