C++ के Relational Operators
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <iostream>
int main() {
int x = 5;
std::cout << (x >= 5) << std::endl; // true: equal counts
std::cout << (x <= 4) << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <iostream>
int main() {
std::cout << ('a' < 'b') << std::endl;
std::cout << ('Z' < 'a') << std::endl; // uppercase codes are smaller
return 0;
}
Login to try C/C++/Java/PHP code in the editor
a == bकी बजायif (a = b)लिखना, जो assign करता है और लगभग हमेशा true होता है।1 < x < 10जैसे comparisons को chain करना, जो1 < xके bool result को 10 से compare करता है और हमेशा true है।- floating-point values को
==से compare करना, जहाँ छोटी rounding errors equal-looking numbers को unequal बना देती हैं।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: