簡(jiǎn)述:在 Delphi 中,有 and 與 or
在 c/c++ 中,有 && 與 ||
在 Lua 中,有 and 與 or
它們3者都表示邏輯運(yùn)算符。但它們是有區(qū)別的。
Delphi中的 and 與 or 不但可以表示邏輯與、邏輯或運(yùn)算符,還可以表示與運(yùn)算以及或運(yùn)算。
示例:
procedure test;
var
lVar: integer;
lVar2: integer;
lVar3: integer;
begin
lVar = 20;
lVar2 = 30;
if (10 = lVar) and (20 = lVar2) then
begin
// do something here.
end
else if (20 = lVar) or (30 = lVar2) then
begin
// do something here.
end;
lVar3 = lVar and lVar2;//這個(gè)就是相當(dāng)于c/c++中的 &
lVar3 = lVar or lVar2;//這個(gè)就相當(dāng)于c/c++中的 |
end;
關(guān)于 c/c++ 中的 &&、||、&、| 在此,我想就不用多說(shuō)了吧。
接下來(lái)說(shuō)說(shuō),Lua中的 and 與 or
在 Lua 中,and 也是邏輯運(yùn)算符,但它的取值很特別:
a and b;它的返回值并不是我們所想象的 true 或者 false.而是:如果 a 為假(即:為 false 或者 nil 時(shí)),則表達(dá)式返回 a。否則就返回 b
同樣 a or b;它返回的也不是true與false。而是:當(dāng) a 為真時(shí),則返回 a 否則返回 b.