Booleans क्या हैं
In this page:
let name: Bool = true
let result = a >= b // == != < > <= >=
let combined = x && y || !z
Declaring Booleans
एक Bool variable या तो true या false रखता है, अक्सर एक comparison के result के रूप में।
उदाहरण: Declaring Booleans
// 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)")
Login to try C/C++/Java/PHP code in the editor
Comparison Operators Produce Booleans
दो values की तुलना ==, !=, <, >, <=, या >= से करना हमेशा एक Bool result produce करता है।
उदाहरण: Comparison Operators Produce Booleans
// 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)")
Login to try C/C++/Java/PHP code in the editor
Combining Booleans with Logical Operators
&& (logical AND), || (logical OR), और ! (logical NOT) operators boolean values को combine या invert करते हैं ज़्यादा complex conditions बनाने के लिए।
उदाहरण: Combining Booleans with Logical Operators
// 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)")
Login to try C/C++/Java/PHP code in the editor
- एक boolean को explicitly
trueसे compare करना, जैसेif isReady == true, cleanerif isReadyके बजाय। - यह मान लेना कि कोई भी non-zero number C की तरह true माना जाता है; Swift का
Boolएक distinct type है और numbers कभी implicitly booleans में convert नहीं होते। - यह भूल जाना कि
&&और||short-circuit करते हैं, इसलिए बाद का एक expression evaluate भी नहीं हो सकता।
Boolमें बिल्कुल दो values हैं:trueऔरfalse।- Comparison operators (
==,<,>, वगैरह)Boolresults 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: