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

C++ के Booleans

bool type exactly दो values में से एक store करता है, true या false, और C++ program में हर conditional statement और loop के decisions drive करता है।
Syntax
cpp
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

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

Boolean Expressions

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

उदाहरण: Boolean Expressions

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

Conditions में Booleans

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

उदाहरण: Booleans in Conditions

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

Comparisons से Boolean Variables

Comparison को inline दोहराने की बजाय इसका result एक named bool variable में store करना conditions को पढ़ना आसान बनाता है और same computed result को कई जगहों पर reuse होने देता है।

उदाहरण: Boolean Variables from Comparisons

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

boolalpha से Booleans Print करना

Default से cout एक bool को 1 या 0 की तरह print करता है, लेकिन boolalpha stream manipulator इसे इसकी बजाय true और false words print कराता है, जो output में ज़्यादा readable हो सकता है।

उदाहरण: Printing Booleans with boolalpha

cpp
#include <iostream>

int main() {
	bool active = true;
	std::cout << active << std::endl; // 1
	std::cout << std::boolalpha << active << std::endl; // true
	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. एक bool print करना और true की उम्मीद करना, जब boolalpha इस्तेमाल न करने पर cout 1 print करता है।
  2. cin से एक bool पढ़ना और true type करना, जो fail होता है; boolalpha के बिना यह 0 या 1 उम्मीद करता है।
  3. यह मान लेना कि सिर्फ 1 true है, जब कोई भी non-zero value 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.