← Back to C++ Course | Chapter 1: Introduction & Basics | Lesson 11 of 15

C++ का bool Data Type

bool type exactly दो values में से एक store करता है, true या false, internally 1 या 0 की तरह represented, और यही वह है जिसमें एक if, while, या for loop की हर condition आखिरकार evaluate होती है।
Syntax
cpp
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

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

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

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

Boolean Expressions

एक boolean expression कोई भी expression है जो true या false evaluate होता है, सबसे common तौर पर दो values को >, <, ==, या != जैसे operators से compare करके बनता है, सीधे एक literal true या false लिखने की बजाय।

उदाहरण: Boolean Expressions

cpp
#include <iostream>

int main() {
	int score = 85;
	bool passed = score >= 60; // comparison, not a literal true/false
	std::cout << passed << std::endl;
	return 0;
}

Conditions में bool

Boolean values ही हैं जिन्हें if, while, और for जैसे control flow statements actually test करते हैं: एक if statement सिर्फ तब अपना block चलाता है जब इसके parentheses के अंदर boolean expression true evaluate हो।

उदाहरण: bool in Conditions

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

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

cpp
#include <iostream>

int main() {
	int value = 5;
	if (value) { // non-zero converts to true
		std::cout << "Treated as true" << std::endl;
	}
	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. यह उम्मीद करना कि cout << true word true print करेगा, जब यह boolalpha इस्तेमाल न करने पर 1 print करता है।
  2. x == 5 की बजाय if (x = 5) लिखना, जो 5 assign करता है और हमेशा true होता है।
  3. यह मान लेना कि कोई non-zero number एक bool की तरह valid नहीं है, जब कोई भी non-zero value true में convert होती है और सिर्फ 0 false है।

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.