खोज…


टिप्पणियों

बुलियन, सत्य और झूठ, सीधे लुआ में हैं। समीक्षा करने के लिए:

  1. एक बूलियन प्रकार है जिसमें बिल्कुल दो मूल्य हैं: true और false
  2. एक सशर्त संदर्भ में ( if , elseif , while , until ), एक बूलियन की आवश्यकता नहीं है। किसी भी अभिव्यक्ति का उपयोग किया जा सकता है।
  3. एक सशर्त संदर्भ में, false और nil रूप में झूठी, और बाकी सब कुछ सच के रूप में गिना जाता है।
  4. हालांकि 3 पहले से ही इसका मतलब है: यदि आप अन्य भाषाओं से आ रहे हैं, तो याद रखें कि 0 और खाली स्ट्रिंग काउंट लुआ में सशर्त संदर्भों में सही हैं।

बूलियन प्रकार

बूलियन और अन्य मूल्य

लुआ के साथ काम करते समय यह बूलियन मूल्यों के बीच अंतर करने के लिए महत्वपूर्ण है true और false और सही या गलत का मूल्यांकन करने वाले मूल्य।

लुआ में केवल दो मूल्य हैं जो असत्य का मूल्यांकन करते हैं: nil और false , जबकि संख्यात्मक 0 सहित अन्य सभी, सत्य का मूल्यांकन करते हैं।

इसका क्या अर्थ है, इसके कुछ उदाहरण:

if 0 then print("0 is true") end --> this will print "true"
if (2 == 3) then print("true") else print("false") end --> this prints "false"
if (2 == 3) == false then print("true") end --> this prints "true"
if (2 == 3) == nil then else print("false") end
--> prints false, because even if nil and false both evaluate to false,
--> they are still different things.

तार्किक संचालन

लुआ में लॉजिकल ऑपरेटर्स जरूरी बूलियन वैल्यू नहीं लौटाते हैं:

and दूसरा मान लौटाएगा यदि पहला मान सत्य का मूल्यांकन करता है;

or दूसरा मान लौटाता है यदि पहला मान गलत का मूल्यांकन करता है;

यह अन्य भाषाओं की तरह ही टर्नरी ऑपरेटर का अनुकरण करना संभव बनाता है:

local var = false and 20 or 30 --> returns 30
local var = true and 20 or 30 --> returns 20
-- in C: false ? 20 : 30

यदि उनका अस्तित्व नहीं है, तो तालिकाओं को आरंभ करने के लिए भी इसका उपयोग किया जा सकता है

tab = tab or {} -- if tab already exists, nothing happens

या अगर बयानों का उपयोग करने से बचने के लिए, कोड को पढ़ना आसान बना देता है

print(debug and "there has been an error") -- prints "false" line if debug is false
debug and print("there has been an error") -- does nothing if debug is false
-- as you can see, the second way is preferable, because it does not output
-- anything if the condition is not met, but it is still possible.
-- also, note that the second expression returns false if debug is false,
-- and whatever print() returns if debug is true (in this case, print returns nil)

यदि चर परिभाषित किए गए हैं तो जाँच करना

एक भी आसानी से जांच कर सकता है कि क्या कोई चर मौजूद है (यदि इसे परिभाषित किया गया है), क्योंकि गैर-विद्यमान चर nil लौटते हैं, जो झूठ का मूल्यांकन करता है।

local tab_1, tab_2 = {}
if tab_1 then print("table 1 exists") end --> prints "table 1 exists"
if tab_2 then print("table 2 exists") end --> prints nothing

एकमात्र मामला जहां यह लागू नहीं होता है, जब एक वैरिएबल मूल्य को false संग्रहीत करता है, तो उस स्थिति में यह तकनीकी रूप से मौजूद होता है लेकिन फिर भी गलत का मूल्यांकन करता है। इस वजह से, यह उन कार्यों को बनाने के लिए एक बुरा डिज़ाइन है जो राज्य या इनपुट के आधार पर false और nil लौटाते हैं। हम अभी भी जाँच कर सकते हैं कि क्या हमारे पास nil या false :

if nil == nil then print("A nil is present") else print("A nil is not present") end
if false == nil then print("A nil is present") else print("A nil is not present") end
-- The output of these calls are:
-- A nil is present!
-- A nil is not present

सशर्त संदर्भ

Lua में सशर्त संदर्भ ( if , elseif , while , until ) एक बूलियन की आवश्यकता नहीं है। कई भाषाओं की तरह, कोई भी Lua मान किसी स्थिति में दिखाई दे सकता है। मूल्यांकन के नियम सरल हैं:

  1. false और nil रूप में झूठी गणना।

  2. बाकी सब कुछ सच है।

    if 1 then
      print("Numbers work.")
    end
    if 0 then
      print("Even 0 is true")
    end
    
    if "strings work" then
      print("Strings work.")
    end
    if "" then
      print("Even the empty string is true.")
    end
    

लॉजिकल ऑपरेटर्स

लुआ में, बूलियंस को तार्किक ऑपरेटरों के माध्यम से हेरफेर किया जा सकता है। इन ऑपरेटरों में शामिल not , and , और or

सरल अभिव्यक्तियों में, परिणाम काफी सरल हैं:

print(not true) --> false
print(not false) --> true
print(true or false) --> true
print(false and true) --> false

वरीयता क्रम

पूर्वता का क्रम गणित संचालक के समान है - , * और + :

  • not
  • तब and
  • तब or

इससे जटिल अभिव्यक्ति हो सकती है:

print(true and false or not false and not true)
print( (true and false) or ((not false) and (not true)) )
    --> these are equivalent, and both evaluate to false

शॉर्ट-कट मूल्यांकन

ऑपरेटरों and / or केवल पहले ऑपरेंड का उपयोग करके मूल्यांकन किया जा सकता है, बशर्ते कि दूसरा अनावश्यक हो:

function a()
    print("a() was called")
    return true
end

function b()
    print("b() was called")
    return false
end

print(a() or b())
    --> a() was called
    --> true
    --  nothing else
print(b() and a())
    --> b() was called
    --> false
    --  nothing else
print(a() and b())
    --> a() was called
    --> b() was called
    --> false

मुहावरेदार सशर्त ऑपरेटर

तार्किक संचालकों की पूर्वता के कारण, शॉर्ट-कट मूल्यांकन और गैर- false और गैर- nil मूल्यों के मूल्यांकन को true मानने की क्षमता, लुआ में एक मुहावरेदार सशर्त ऑपरेटर उपलब्ध है:

function a()
    print("a() was called")
    return false
end
function b()
    print("b() was called")
    return true
end
function c()
    print("c() was called")
    return 7
end

print(a() and b() or c())
    --> a() was called
    --> c() was called
    --> 7
    
print(b() and c() or a())
    --> b() was called
    --> c() was called
    --> 7

इसके अलावा, की प्रकृति के कारण x and a or b संरचना, a कभी नहीं करता है, तो यह करने के लिए मूल्यांकन करता है लौटा दी जाएगी false , यह सशर्त तो हमेशा वापस आ जाएगी b कोई बात नहीं क्या x है।

print(true and false or 1)  -- outputs 1

सत्य सारणी

लुआ में लॉजिकल ऑपरेटर्स बूलियन को "वापस" नहीं करते हैं, लेकिन उनके तर्कों में से एक है। का उपयोग nil झूठे और सच के लिए संख्या के लिए, यहाँ उनका व्यवहार है।

print(nil and nil)       -- nil
print(nil and 2)         -- nil
print(1 and nil)         -- nil
print(1 and 2)           -- 2

print(nil or nil)        -- nil
print(nil or 2)          -- 2
print(1 or nil)          -- 1
print(1 or 2)            -- 1

जैसा कि आप देख सकते हैं, लुआ हमेशा पहला मान लौटाएगा जो चेक को विफल या सफल बनाता है। यहाँ सच सारणी है कि दिखा रहा है।

  x  |  y  || and            x  |  y  || or
------------------         ------------------
false|false||  x           false|false||  y   
false|true ||  x           false|true ||  y   
true |false||  y           true |false||  x   
true |true ||  y           true |true ||  x

जिन लोगों को इसकी आवश्यकता है, उनके लिए यहां इन तार्किक ऑपरेटरों का प्रतिनिधित्व करने वाले दो कार्य हैं।

function exampleAnd(value1, value2)
  if value1 then
    return value2
  end
  return value1
end

function exampleOr(value1, value2)
  if value1 then
    return value1
  end
  return value2
end

'और' या 'तार्किक संचालकों' के साथ टर्नरी ऑपरेटर का अनुकरण करना।

लुआ में, तार्किक ऑपरेटर and एक or एक ऑपरेंड को बूलियन परिणाम के बजाय परिणाम के रूप में देता है। परिणामस्वरूप, भाषा में एक 'वास्तविक' टर्नरी ऑपरेटर नहीं होने के बावजूद इस तंत्र का दोहन टर्नरी ऑपरेटर के व्यवहार का अनुकरण करने के लिए किया जा सकता है।

वाक्य - विन्यास

हालत और truthy_expr या falsey_expr

चर असाइनमेंट / इनिशियलाइज़ेशन में उपयोग करें

local drink = (fruit == "apple") and "apple juice" or "water"

टेबल कंस्ट्रक्टर में उपयोग करें

local menu =
{
  meal  = vegan and "carrot" or "steak",
  drink = vegan and "tea"    or "chicken soup"
}

फ़ंक्शन तर्क के रूप में उपयोग करें

print(age > 18 and "beer" or "fruit punch")

रिटर्न स्टेटमेंट में उपयोग करें

function get_gradestring(student)
  return student.grade > 60 and "pass" or "fail"
end

चेतावनी

ऐसी परिस्थितियां हैं जहां इस तंत्र में वांछित व्यवहार नहीं है। इस मामले पर विचार करें

local var = true and false or "should not happen"

एक 'वास्तविक' टर्नरी ऑपरेटर में, var का अपेक्षित मान false । हालांकि, लूआ में, and मूल्यांकन 'के माध्यम से गिरता है' क्योंकि दूसरा ऑपरेंड गलत है। परिणामस्वरूप var समाप्त होता should not happen इसके बजाय should not happen चाहिए।

इस समस्या के लिए दो संभावित वर्कअराउंड, इस अभिव्यक्ति को रिफ्लेक्टर करते हैं ताकि मध्य ऑपरेंड गलत न हो। जैसे।

local var = not true and "should not happen" or false

या वैकल्पिक रूप से, शास्त्रीय का उपयोग if then else निर्माण।



Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow