C ++ रिलेशनल और लॉजिकल ऑपरेटर्स (उदाहरणों के साथ)

इस ट्यूटोरियल में, हम उदाहरणों की मदद से रिलेशनल और लॉजिकल ऑपरेटर्स के बारे में जानेंगे।

C ++ में, रिलेशनल और लॉजिकल ऑपरेटर दो या दो से अधिक ऑपरेंड की तुलना करते हैं और trueया तो falseमान या रिटर्न देते हैं।

हम निर्णय लेने में इन ऑपरेटरों का उपयोग करते हैं।

सी ++ रिलेशनल ऑपरेटर्स

एक रिलेशनल ऑपरेटर का उपयोग दो ऑपरेंड के बीच संबंधों की जांच करने के लिए किया जाता है। उदाहरण के लिए,

 // checks if a is greater than b a> b;

यहां, >एक रिलेशनल ऑपरेटर है। यह जाँचता है कि क्या बी से अधिक है या नहीं।

यदि संबंध सत्य है , तो यह 1 वापस आता है जबकि यदि संबंध गलत है , तो यह 0 देता है ।

निम्न तालिका C ++ में प्रयुक्त रिलेशनल ऑपरेटरों को सारांशित करती है।

ऑपरेटर अर्थ उदाहरण
== के बराबर है 3 == 5हमें असत्य देता है
!= असमान 3 != 5हमें सच देता है
> से अधिक 3> 5हमें असत्य देता है
< से कम 3 < 5हमें सच देता है
>= इससे बड़ा या इसके बराबर 3>= 5हमें असत्य दो
<= से कम या बराबर 3 <= 5हमें सच देता है

== संचालक

==ऑपरेटर रिटर्न के बराबर

  • true - यदि दोनों ऑपरेशंस समान या समान हैं
  • false - यदि ऑपरेंड असमान हैं

उदाहरण के लिए,

 int x = 10; int y = 15; int z = 10; x == y // false x == z // true

नोट: रिलेशनल ऑपरेटर ==असाइनमेंट ऑपरेटर के समान नहीं है =। असाइनमेंट ऑपरेटर =किसी वैरिएबल, कंटीन्यू, एरे, या वेक्टर के लिए एक मान प्रदान करता है। यह दो ऑपरेंड की तुलना नहीं करता है।

= = संचालक

!=ऑपरेटर रिटर्न के बराबर नहीं

  • true - यदि दोनों ऑपरेंड असमान हैं
  • false - यदि दोनों ऑपरेशंस समान हैं।

उदाहरण के लिए,

 int x = 10; int y = 15; int z = 10; x != y // true x != z // false

> संचालक

>ऑपरेटर रिटर्न से अधिक है

  • true - यदि बाएं ऑपरेंड दाएं से अधिक है
  • false - यदि बाएं ऑपरेंड दाएं से कम है

उदाहरण के लिए,

 int x = 10; int y = 15; x> y // false y> x // true

<संचालक

ऑपरेटर से कम <रिटर्न

  • true - यदि बाएं ऑपरेंड दाएं से कम है
  • false - यदि बाएं ऑपरेंड दाएं से अधिक है

उदाहरण के लिए,

 int x = 10; int y = 15; x < y // true y < x // false

> = संचालक

>=ऑपरेटर रिटर्न के बराबर या उससे अधिक

  • true - यदि बाएं ऑपरेंड या तो दाईं ओर से अधिक या बराबर है
  • false - यदि बाएं ऑपरेंड दाएं से कम है

उदाहरण के लिए,

 int x = 10; int y = 15; int z = 10; x>= y // false y>= x // true z>= x // true

<= संचालक

The less than or equal to operator <= returns

  • true - if the left operand is either less than or equal to the right
  • false - if the left operand is greater than right

For example,

 int x = 10; int y = 15; x> y // false y> x // true

In order to learn how relational operators can be used with strings, refer to our tutorial here.

C++ Logical Operators

We use logical operators to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0.

Operator Example Meaning
&& expression1 && expression 2 Logical AND.
true only if all the operands are true.
|| expression1 || expression 2 Logical OR.
true if at least one of the operands is true.
! !expression Logical NOT.
true only if the operand is false.

C++ Logical AND Operator

The logical AND operator && returns

  • true - if and only if all the operands are true.
  • false - if one or more operands are false.

Truth Table of && Operator

Let a and b be two operands. 0 represents false while 1 represents true. Then,

a b a && b
0 0 0
0 1 0
1 0 0
1 1 1

As we can see from the truth table above, the && operator returns true only if both a and b are true.

Note: The Logical AND operator && should not be confused with the Bitwise AND operator &.

Example 1: C++ OR Operator

 // C++ program demonstrating && operator truth table #include using namespace std; int main() ( int a = 5; int b = 9; // false && false = false cout < b)) << endl; // false && true = false cout << ((a == 0) && (a < b)) << endl; // true && false = false cout < b)) << endl; // true && true = true cout << ((a == 5) && (a < b)) << endl; return 0; )

Output

 0 0 0 1

In this program, we declare and initialize two int variables a and b with the values 5 and 9 respectively. We then print a logical expression

 ((a == 0) && (a> b))

Here, a == 0 evaluates to false as the value of a is 5. a> b is also false since the value of a is less than that of b. We then use the AND operator && to combine these two expressions.

From the truth table of && operator, we know that false && false (i.e. 0 && 0) results in an evaluation of false (0). This is the result we get in the output.

Similarly, we evaluate three other expressions that fully demonstrate the truth table of the && operator.

C++ Logical OR Operator

The logical OR operator || returns

  • true - if one or more of the operands are true.
  • false - if and only if all the operands are false.

Truth Table of || Operator

Let a and b be two operands. Then,

a b a || b
0 0 0
0 1 1
1 0 1
1 1 1

As we can see from the truth table above, the || operator returns false only if both a and b are false.

Example 2: C++ OR Operator

 // C++ program demonstrating || operator truth table #include using namespace std; int main() ( int a = 5; int b = 9; // false && false = false cout < b)) << endl; // false && true = true cout << ((a == 0) || (a < b)) << endl; // true && false = true cout < b)) << endl; // true && true = true cout << ((a == 5) || (a < b)) << endl; return 0; )

Output

 0 1 1 1

In this program, we declare and initialize two int variables a and b with the values 5 and 9 respectively. We then print a logical expression

 ((a == 0) || (a> b))

Here, a == 0 evaluates to false as the value of a is 5. a> b is also false since the value of a is less than that of b. We then use the OR operator || to combine these two expressions.

From the truth table of || operator, we know that false || false (i.e. 0 || 0) results in an evaluation of false (0). This is the result we get in the output.

Similarly, we evaluate three other expressions that fully demonstrate the truth table of || operator.

C++ Logical NOT Operator !

The logical NOT operator ! is a unary operator i.e. it takes only one operand.

It returns true when the operand is false, and false when the operand is true.

सत्य की तालिका! ऑपरेटर

आज्ञा देना एक ऑपरेंड। फिर,

उदाहरण 3: C ++! ऑपरेटर

 // C++ program demonstrating ! operator truth table #include using namespace std; int main() ( int a = 5; // !false = true cout << !(a == 0) << endl; // !true = false cout << !(a == 5) << endl; return 0; )

आउटपुट

 १ ०

इस कार्यक्रम में, हम intमूल्य के साथ एक चर घोषित करते हैं और शुरू करते हैं 5। हम फिर एक तार्किक अभिव्यक्ति छापते हैं

 !(a == 0) 

यहाँ, एक के मान के रूप में a == 0मूल्यांकन falseकरता है 5। हालाँकि, हम NOT ऑपरेटर का उपयोग नहीं !करते हैं a == 0। चूंकि a == 0मूल्यांकन करने के लिए false, !ऑपरेटर परिणाम का परिणाम निकालता है a == 0और अंतिम परिणाम होता है true

इसी तरह, अभिव्यक्ति !(a == 5)अंततः लौटती है falseक्योंकि a == 5है true

दिलचस्प लेख...