← Back to C++ Course | Chapter 3: Operators | Lesson 3 of 9

C++ के Relational Operators

Relational operators दो values compare करते हैं, यह पूछने जैसा कि कौन लंबा है। जवाब हमेशा true या false होता है।
Syntax
cpp
bool result = value1 == value2;  // also != > < >= <=

Equality और Inequality

== check करता है कि दो values equal हैं या नहीं और != check करता है कि वे अलग हैं या नहीं, और दोनों हमेशा values की बजाय एक boolean result (true या false) produce करते हैं।

एक बहुत common beginner mistake है एक condition के अंदर == की बजाय एक single = लिखना, जो compare करने की बजाय चुपचाप assign कर देता है।

उदाहरण: Equality and Inequality

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int a = 5, b = 3;
	// Print to the console with cout
	std::cout << (a == b) << std::endl;
	// Print to the console with cout
	std::cout << (a != b) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Greater than और Less than

> और < test करते हैं कि left-hand value right-hand value से strictly greater है या strictly less है, जो strict है इस मायने में कि equal values दोनों के लिए false evaluate होती हैं।

ये C++ code भर में sorting logic और range checks की basis हैं।

उदाहरण: Greater than and Less than

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << (5 > 3) << std::endl;
	// Print to the console with cout
	std::cout << (5 < 3) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Greater than or Equal, और Less than or Equal

>= और <= equality भी accept करने के लिए > और < को extend करते हैं, इसलिए x >= 5 जब भी x 5 या इससे बड़ा कुछ हो true है।

Strict और inclusive versions के बीच सही चुनना off-by-one errors से बचने के लिए critical है, खासकर loop bounds और array index checks में।

उदाहरण: Greater than or Equal, and Less than or Equal

cpp
#include <iostream>

int main() {
	int x = 5;
	std::cout << (x >= 5) << std::endl; // true: equal counts
	std::cout << (x <= 4) << std::endl;
	return 0;
}

Numbers पर Relational Checks

Relational operators ही हैं जो if statements को उनकी decision-making power देते हैं — if (score >= passingGrade) जैसा एक expression true या false evaluate होता है और decide करता है कि code की कौन सी branch actually चलती है।

एक real C++ program में conditional logic का लगभग हर piece इन comparisons के combinations से बना है।

उदाहरण: Relational Checks on Numbers

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int score = 75, passingGrade = 60;
	// Check whether score >= passingGrade
	if (score >= passingGrade) {
		// Print to the console with cout
		std::cout << "Passed" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Characters पर Relational Checks

char values compare करना उनके underlying ASCII numeric codes compare करता है, इसलिए a < b true है क्योंकि a का code b से छोटा है, और importantly, सारे uppercase letters सारे lowercase letters से छोटे codes रखते हैं, मतलब Z < a भी true है।

यही ASCII-based ordering है जिसकी वजह से mixed-case text की naive alphabetical sorting case normalize किए बिना surprising results produce कर सकती है।

उदाहरण: Relational Checks on Characters

cpp
#include <iostream>

int main() {
	std::cout << ('a' < 'b') << std::endl;
	std::cout << ('Z' < 'a') << std::endl; // uppercase codes are smaller
	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. a == b की बजाय if (a = b) लिखना, जो assign करता है और लगभग हमेशा true होता है।
  2. 1 < x < 10 जैसे comparisons को chain करना, जो 1 < x के bool result को 10 से compare करता है और हमेशा true है।
  3. floating-point values को == से compare करना, जहाँ छोटी rounding errors equal-looking numbers को unequal बना देती हैं।

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.