delphi - mathematical 'not' of integer -
why delphi perform mathematical 'not' of integer rather force cast boolean value in while looping ? example
var myint:integer; ... myint:=1; while not myint=5 begin myint:=myint+1; showmessage('myint : '+inttostr(myint)); end;
your expression uses 2 operators: not
, =
. in order understand how parsed need consult table of operator precedence.
operators precedence ---------------------------- @ first (highest) not ---------------------------- * second / div mod , shl shr ---------------------------- + third - or xor ---------------------------- = fourth (lowest) <> < > <= >= in ----------------------------
this shows not
has highest precedence of operators, , of higher precedence =
. means expression parsed if were:
(not myint) = 5
in expression, because bound integral variable, not
biwise negation.
in order result desire must use parentheses indicate wish perform equality test before not
:
not (myint = 5)
now, in expression, (myint = 5)
logical expression , not
operator logical negation.
the answer question of nature found in table of operator precedence , pay dividends become familiar table. table tell how expression omits parentheses parsed.
finally rudy points out, specific expression cleanly written using <>
operator:
myint <> 5
Comments
Post a Comment