C++ के Booleans
In this page:
bool variable_name = true; // or false
bool variable_name = expression; // e.g. x > 5
bool Type
bool type exactly दो possible values में से एक represent करता है, true या false, और logical values के लिए C++ का dedicated type है, इस्तेमाल तब होता है जब information का कोई piece एक simple yes-or-no flag हो।
उदाहरण: The bool Type
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare isSubscribed and set it to true
bool isSubscribed = true;
// Print to the console with cout
std::cout << isSubscribed << std::endl;
// Return 0 to signal that the program finished successfully
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 std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare x and set it to 10
int x = 10;
// Declare result and set it to x > 5
bool result = x > 5;
// Print to the console with cout
std::cout << result << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Conditions में Booleans
Boolean values ही हैं जिन्हें if, while, और for जैसे control flow statements actually test करते हैं: एक if statement सिर्फ तब अपना block चलाता है जब इसके parentheses के अंदर boolean expression true evaluate हो।
उदाहरण: Booleans in Conditions
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare isOpen and set it to true
bool isOpen = true;
// Check whether isOpen
if (isOpen) {
// Print to the console with cout
std::cout << "Store is open" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Comparisons से Boolean Variables
Comparison को inline दोहराने की बजाय इसका result एक named bool variable में store करना conditions को पढ़ना आसान बनाता है और same computed result को कई जगहों पर reuse होने देता है।
उदाहरण: Boolean Variables from Comparisons
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare score and set it to 70
int score = 70;
bool passed = score >= 60;
// Check whether passed
if (passed) {
// Print to the console with cout
std::cout << "Passed" << std::endl;
}
// Print to the console with cout
std::cout << passed << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
boolalpha से Booleans Print करना
Default से cout एक bool को 1 या 0 की तरह print करता है, लेकिन boolalpha stream manipulator इसे इसकी बजाय true और false words print कराता है, जो output में ज़्यादा readable हो सकता है।
उदाहरण: Printing Booleans with boolalpha
#include <iostream>
int main() {
bool active = true;
std::cout << active << std::endl; // 1
std::cout << std::boolalpha << active << std::endl; // true
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक bool print करना और
trueकी उम्मीद करना, जबboolalphaइस्तेमाल न करने परcout1print करता है। cinसे एक bool पढ़ना औरtruetype करना, जो fail होता है;boolalphaके बिना यह 0 या 1 उम्मीद करता है।- यह मान लेना कि सिर्फ 1 true है, जब कोई भी non-zero value true है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: