← Back to C++ Course | Chapter 4: Control Flow | Lesson 2 of 13

C++ के Boolean Expressions

एक boolean expression values और operators combine करके true या false produce करता है, और C++ किसी भी non-bool value को implicitly convert करता है जब एक condition की तरह इस्तेमाल हो, zero को false की तरह और बाकी सब true की तरह treat करते हुए।
Syntax
cpp
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

cpp
// 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;
}

Expressions में Comparison Operators

==, !=, <, >, <=, और >= जैसे comparison operators एक boolean expression के सबसे common building blocks हैं, हर एक दो values compare करता है और true या false produce करता है।

उदाहरण: Comparison Operators in Expressions

cpp
// 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;
}

Expressions में Logical Operators

Logical operators &&, ||, और ! simpler boolean expressions को एक single बड़े expression में combine या invert करते हैं, एक condition को एक साथ कई factors पर depend करने देते हुए।

उदाहरण: Logical Operators in Expressions

cpp
// 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;
}

Expressions में Implicit Conversion

जहाँ एक boolean expression expected है वहाँ इस्तेमाल की गई कोई भी non-bool value implicitly convert होती है: zero (या एक empty/null value) false में convert होता है, और बाकी सब कुछ true में।

उदाहरण: Implicit Conversion in Expressions

cpp
#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;
}

Complex Expressions Evaluate करना

एक boolean expression कई comparisons और logical operators को साथ combine कर सकता है, और operator precedence समझना (comparisons && और || से tighter bind करते हैं) intended result पाने के लिए important है।

उदाहरण: Evaluating Complex Expressions

cpp
// 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;
}
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. x == 5 का मतलब होने पर एक condition के अंदर x = 5 लिखना।
  2. यह उम्मीद करना कि 1 < x < 10 एक range test करेगा, जब यह (1 < x) < 10 की तरह evaluate होता है और हमेशा true है।
  3. यह मान लेना कि एक non-bool value को compare करना ज़रूरी है, जब एक expression में कोई भी non-zero number true माना जाता है।

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.