← Back to Swift Course | Chapter 2: Variables & Types | Lesson 6 of 8

Booleans क्या हैं

एक boolean एक value है जो कभी सिर्फ true या false हो सकती है, किसी सवाल का हां या ना जवाब देने जैसा।
Syntax
swift
let name: Bool = true
let result = a >= b            // == != < > <= >=
let combined = x && y || !z

Declaring Booleans

एक Bool variable या तो true या false रखता है, अक्सर एक comparison के result के रूप में।

उदाहरण: Declaring Booleans

markup
// Declare a constant `isSwiftFun`, set to `true`
let isSwiftFun = true
// Declare a constant `isDifficult`, set to `false`
let isDifficult = false
// Print "Is Swift fun? \(isSwiftFun)" to the console
print("Is Swift fun? \(isSwiftFun)")
// Print "Is it too difficult? \(isDifficult)" to the console
print("Is it too difficult? \(isDifficult)")

Comparison Operators Produce Booleans

दो values की तुलना ==, !=, <, >, <=, या >= से करना हमेशा एक Bool result produce करता है।

उदाहरण: Comparison Operators Produce Booleans

markup
// Declare a constant `age`, set to `20`
let age = 20
// Declare a constant `isAdult`, set to `age >= 18`
let isAdult = age >= 18
// Print "Is an adult: \(isAdult)" to the console
print("Is an adult: \(isAdult)")

Combining Booleans with Logical Operators

&& (logical AND), || (logical OR), और ! (logical NOT) operators boolean values को combine या invert करते हैं ज़्यादा complex conditions बनाने के लिए।

उदाहरण: Combining Booleans with Logical Operators

markup
// Declare a constant `hasTicket`, set to `true`
let hasTicket = true
// Declare a constant `hasID`, set to `false`
let hasID = false
// Declare a constant `canEnter`, set to `hasTicket && hasID`
let canEnter = hasTicket && hasID
// Print "Can enter: \(canEnter)" to the console
print("Can enter: \(canEnter)")
// Print "Missing ID: \(!hasID)" to the console
print("Missing ID: \(!hasID)")
Related Topics
{# 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. एक boolean को explicitly true से compare करना, जैसे if isReady == true, cleaner if isReady के बजाय।
  2. यह मान लेना कि कोई भी non-zero number C की तरह true माना जाता है; Swift का Bool एक distinct type है और numbers कभी implicitly booleans में convert नहीं होते।
  3. यह भूल जाना कि && और || short-circuit करते हैं, इसलिए बाद का एक expression evaluate भी नहीं हो सकता।
चैप्टर सारांश
  • Bool में बिल्कुल दो values हैं: true और false।
  • Comparison operators (==, <, >, वगैरह) Bool results produce करते हैं।
  • Logical operators && (and), || (or), और ! (not) boolean values combine करते हैं।
  • C के विपरीत, Swift कभी numbers को implicitly true या false नहीं मानता।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 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.