C++ के Operators
In this page:
result = operand1 operator operand2;
// operator: arithmetic (+ - * / %), relational (== != < >), logical (&& || !)
Operators क्या हैं?
एक operator एक special symbol है जो एक या ज़्यादा values पर, जिन्हें operands कहते हैं, एक operation perform करता है, एक नई value produce करते हुए -- उदाहरण के लिए, + symbol दो numeric operands को साथ add करता है।
उदाहरण: What are Operators?
#include <iostream>
int main() {
int result = 5 + 3; // + is the operator, 5 and 3 are operands
std::cout << result << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
C++ में Operators की Categories
C++ operators को इस आधार पर categories में बांटता है कि वे किस तरह का result produce करते हैं: arithmetic operators numbers compute करते हैं, relational operators values compare करते हैं और booleans produce करते हैं, और logical operators boolean values combine करते हैं।
उदाहरण: Categories of Operators in C++
#include <iostream>
int main() {
int sum = 5 + 3; // arithmetic
bool isGreater = 5 > 3; // relational
bool bothTrue = true && isGreater; // logical
std::cout << sum << " " << isGreater << " " << bothTrue << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Operators और Operands
हर operator operands पर काम करता है -- + और * जैसे ज़्यादातर operators binary हैं और उन्हें दो operands चाहिए, जबकि ++ और unary - जैसे कुछ unary हैं और सिर्फ एक single operand पर काम करते हैं।
उदाहरण: Operators and Operands
#include <iostream>
int main() {
int a = 5, b = 3;
int sum = a + b; // binary: two operands
int negative = -a; // unary: one operand
std::cout << sum << " " << negative << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Expressions में Operators Combine करना
अलग categories के operators अक्सर एक single expression में combine होते हैं, जैसे एक if statement के अंदर इस्तेमाल होने वाली एक condition बनाने के लिए arithmetic और relational operators मिलाना।
उदाहरण: Combining Operators in Expressions
#include <iostream>
int main() {
int price = 50, quantity = 3;
if (price * quantity > 100) { // arithmetic + relational combined
std::cout << "Order qualifies for discount" << std::endl;
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Operator Precedence का Preview
जब एक expression कई operators मिलाता है, C++ उन्हें एक fixed precedence order के हिसाब से evaluate करता है -- default से multiplication और division addition और subtraction से पहले चलते हैं, और parentheses हमेशा उस default order को override कर सकते हैं।
उदाहरण: Operator Precedence Preview
#include <iostream>
int main() {
int result = 2 + 3 * 4; // multiplication runs first: 14
int overridden = (2 + 3) * 4; // parentheses override: 20
std::cout << result << " " << overridden << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- conditions में
=(assignment) को==(comparison) से confuse करना। - यह उम्मीद करना कि
7 / 2में integer division 3.5 देगा, जब दोनों operands ints हैं इसलिए result 3 है। - यह मान लेना कि
a + b * cleft to right evaluate होता है, जब*की precedence+से ज़्यादा है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: