C++ के Boolean Expressions
In this page:
bool result = (left_value comparison_operator right_value);
bool result = (condition1) && (condition2);
एक Boolean Expression बनाना
एक boolean expression values या variables पर apply किए comparison या logical operators से बनता है, और पूरा expression एक single true या false result में collapse हो जाता है।
उदाहरण: Building a Boolean Expression
// 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) && (x < 20)
bool result = (x > 5) && (x < 20);
// 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
Expressions में Comparison Operators
==, !=, <, >, <=, और >= जैसे comparison operators एक boolean expression के सबसे common building blocks हैं, हर एक दो values compare करता है और true या false produce करता है।
उदाहरण: Comparison Operators in Expressions
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int a = 5, b = 10;
// Print to the console with cout
std::cout << (a == b) << " " << (a < b) << " " << (a >= b) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Expressions में Logical Operators
Logical operators &&, ||, और ! simpler boolean expressions को एक single बड़े expression में combine या invert करते हैं, एक condition को एक साथ कई factors पर depend करने देते हुए।
उदाहरण: Logical Operators in Expressions
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
bool loggedIn = true, isAdmin = false;
// Declare canEdit and set it to loggedIn && !isAdmin
bool canEdit = loggedIn && !isAdmin;
// Print to the console with cout
std::cout << canEdit << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Expressions में Implicit Conversion
जहाँ एक boolean expression expected है वहाँ इस्तेमाल की गई कोई भी non-bool value implicitly convert होती है: zero (या एक empty/null value) false में convert होता है, और बाकी सब कुछ true में।
उदाहरण: Implicit Conversion in Expressions
#include <iostream>
int main() {
int count = 0;
if (count) { // 0 converts to false
std::cout << "Has items" << std::endl;
} else {
std::cout << "Empty" << std::endl;
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Complex Expressions Evaluate करना
एक boolean expression कई comparisons और logical operators को साथ combine कर सकता है, और operator precedence समझना (comparisons && और || से tighter bind करते हैं) intended result पाने के लिए important है।
उदाहरण: Evaluating Complex Expressions
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare age and set it to 25
int age = 25;
// Declare hasLicense and set it to true
bool hasLicense = true;
bool canDrive = age >= 18 && hasLicense || false;
// Print to the console with cout
std::cout << canDrive << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
x == 5का मतलब होने पर एक condition के अंदरx = 5लिखना।- यह उम्मीद करना कि
1 < x < 10एक range test करेगा, जब यह(1 < x) < 10की तरह evaluate होता है और हमेशा true है। - यह मान लेना कि एक non-bool value को compare करना ज़रूरी है, जब एक expression में कोई भी non-zero number true माना जाता है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: