Logical Operators की जानकारी
and, or और not truthiness पर काम करते हैं और आपको सिर्फ true या false नहीं बल्कि short-circuit values देते हैं।
In this page:
Logical Operators
and operator अपना पहला operand return करता है अगर वह falsy हो, नहीं तो इसका दूसरा। or operator अपना पहला operand return करता है अगर यह truthy हो, नहीं तो इसका दूसरा। इस वजह से, a and b or c एक ternary conditional की तरह act करता है, और x or default एक default value supply करता है। not हमेशा एक real boolean return करता है।
Note:
and/or ternary तब fail होता है जब middle value false या nil हो सकती है, इसलिए उस case में if इस्तेमाल करें।
उदाहरण: Logical operators
print(nil or "default", false and "never")
print(1 and 2, nil and 2, 0 or 5)
local age = 20
print(age >= 18 and "adult" or "minor")
print(not nil, not 0)
print(false and nil or "fallthrough")
-- Output:
-- default false
-- 2 nil 0
-- adult
-- true false
-- fallthrough
Login to try C/C++/Java/PHP code in the editor
आम गलतियां
- यह उम्मीद करना कि and/or हमेशा booleans return करेंगे
- a and b or c इस्तेमाल करना जब b false हो सकता है
- दूसरी languages से && और || इस्तेमाल करना
चैप्टर सारांश
- and अपना पहला falsy operand या आखिरी वाला return करता है
- or अपना पहला truthy operand return करता है
- x or default एक default value देता है
- not हमेशा एक boolean देता है
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: