C++ का bool Data Type
In this page:
bool variable_name = true; // or false
std::cout << std::boolalpha << variable_name;
bool Type
bool type exactly दो possible values में से एक represent करता है, true या false, और यह logical values के लिए C++ का dedicated type है, C के उलट जो historically same purpose के लिए plain integers इस्तेमाल करता था।
उदाहरण: The bool Type
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare isReady and set it to true
bool isReady = true;
// Declare isDone and set it to false
bool isDone = false;
// Print to the console with cout
std::cout << isReady << " " << isDone << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
bool 1 या 0 की तरह Print होता है
Default से, cout एक bool value को true के लिए integer 1 या false के लिए 0 की तरह print करता है, क्योंकि bool internally एक छोटे integer की तरह stored है, हालांकि boolalpha इसकी बजाय words print करने के लिए इस्तेमाल किया जा सकता है।
उदाहरण: bool Prints as 1 or 0
#include <iostream>
int main() {
bool active = true;
std::cout << active << std::endl; // prints 1
std::cout << std::boolalpha << active << std::endl; // prints true
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Boolean Expressions
एक boolean expression कोई भी expression है जो true या false evaluate होता है, सबसे common तौर पर दो values को >, <, ==, या != जैसे operators से compare करके बनता है, सीधे एक literal true या false लिखने की बजाय।
उदाहरण: Boolean Expressions
#include <iostream>
int main() {
int score = 85;
bool passed = score >= 60; // comparison, not a literal true/false
std::cout << passed << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Conditions में bool
Boolean values ही हैं जिन्हें if, while, और for जैसे control flow statements actually test करते हैं: एक if statement सिर्फ तब अपना block चलाता है जब इसके parentheses के अंदर boolean expression true evaluate हो।
उदाहरण: bool in Conditions
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare hasStock and set it to true
bool hasStock = true;
// Check whether hasStock
if (hasStock) {
// Print to the console with cout
std::cout << "In stock" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
bool में Implicit Conversion
C++ में, जहाँ एक bool expected है वहाँ इस्तेमाल की गई कोई भी non-bool value implicitly convert होती है: zero false में convert होता है और कोई भी non-zero numeric value true में, यही वजह है कि एक if statement सीधे एक plain int test कर सकता है।
उदाहरण: Implicit Conversion to bool
#include <iostream>
int main() {
int value = 5;
if (value) { // non-zero converts to true
std::cout << "Treated as true" << std::endl;
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- यह उम्मीद करना कि
cout << trueword true print करेगा, जब यहboolalphaइस्तेमाल न करने पर1print करता है। x == 5की बजायif (x = 5)लिखना, जो 5 assign करता है और हमेशा true होता है।- यह मान लेना कि कोई non-zero number एक bool की तरह valid नहीं है, जब कोई भी non-zero value
trueमें convert होती है और सिर्फ 0falseहै।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: