← Back to Lua Course | Chapter 2: Variables & Types | Lesson 7 of 7

Logical Operators की जानकारी

and, or और not truthiness पर काम करते हैं और आपको सिर्फ true या false नहीं बल्कि short-circuit values देते हैं।

In this page:

  1. Logical Operators

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

lua
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
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह उम्मीद करना कि and/or हमेशा booleans return करेंगे
  2. a and b or c इस्तेमाल करना जब b false हो सकता है
  3. दूसरी 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.