C++ Assignment Operator
ऐसे Operators जो किसी वैल्यू को किसी variable में store करने के लिए काम में आते हैं, उन्हे Assignment Operator कहा जाता हैं।
Example 1:-
#include <iostream>
using namespace std;
int main() {
int a, b;
// 2 is assigned to a
a = 2;
// 7 is assigned to b
b = 7;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "\nAfter a += b;" << endl;
// assigning the sum of a and b to a
a += b; // a = a +b
cout << "a = " << a << endl;
return 0;
}
Output:-
a = 2
b = 7
After a += b;
a = 9
Example #2:-
#include <iostream>
using namespace std;
int main()
{
int a = 10; //= Assignment Operator
cout << "a = " << a;
a += 2; // += Assignment Operator
cout << "\nThe Result of a += 2 is " << a;
a = 10;
a -= 2; // -= Assignment Operator
cout << "\nThe Result of a -= 2 is " << a;
a = 10;
a *= 2; // *= Assignment Operator
cout << "\nThe Result of a *= 2 is " << a;
a = 10;
a /= 2; // /= Assignment Operator
cout << "\nThe Result of a /= 2 is " << a;
a = 10;
a %= 4; // %= Assignment Operator
cout << "\nThe Result of a %= 4 is " << a;
return 0;
}
Output:-
a = 10
The Result of a += 2 is 12
The Result of a -= 2 is 8
The Result of a *= 2 is 20
The Result of a /= 2 is 5
The Result of a %= 4 is 2
C++ Increment and Decrement Operators
C++ इंक्रीमेंट और डिक्रीमेंट ऑपरेटर्स भी प्रदान करता है, ऐसे Operator जो किसी variable की वैल्यू में एक जोड़ने या घटाने के काम में आते हैं,इन्हे क्रमश: increment(क्रीमेंट) ++
और Decrement(डिक्रीमेंट) --
ऑपरेटर्स कहते हैं।
Operator | Description (both are unary) |
++ | ++ ऑपरेंड का मान 1 से बढ़ा देता है |
-- | -- ऑपरेंड का मान 1 से घटा देता है |
उपरोक्त Operator को दो प्रकार से प्रयोग किया जाता हैं Pre(प्री) एवं Post(पोस्ट)।
Pre(प्री) Increment Operator को Operand के पहले लगाया जाता हैं, (जैसे ++p
)।
Post(पोस्ट) Increment Operator को Operand के बाद लगाया जाता हैं( जैसे ++p
)।
जब इन ऑपरेटर्स को अकेले प्रयोग किया जाता है तो दोनों के परिणाम समान ही होते हैं। वहीं यदि इनका प्रयोग किसी एक्सप्रेशन में किया जाता है तो दोनों के परिणाम अलग-अलग हो सकते हैं।
Pre Increment Operator
जब Expression(एक्सप्रेशन) में इन्हें प्री ऑपरेटर की भांति प्रयोग किया जाता है तो इनमें पहले इंक्रीमेंट होता है। और बाद में उस बढ़ी हुई वैल्यू का Expression(एक्सप्रेशन) में प्रयोग किया जाता है।
Example #1: pre Increment Operator
#include <iostream>
using namespace std;
int main()
{
int q, p = 10;
q = ++p;
cout <<"The value of q is "<< q<<endl;
cout <<"The value of p is "<< p;
return 0;
}
Output:-
The value of q is 11
The value of p is 11
Example #2: post Increment Operator
#include <iostream>
using namespace std;
int main()
{
int q, p = 10;
q = p++;
cout <<"The value of q is "<< q<<endl;
cout <<"The value of p is "<< p;
return 0;
}
Output:-
The value of q is 10
The value of p is 11
Example #3: post Decrement Operator
#include <iostream>
using namespace std;
int main()
{
int q, p = 10;
q = p--;
cout <<"The value of q is "<< q<<endl;
cout <<"The value of p is "<< p;
return 0;
}
Output:-
The value of q is 10
The value of p is 9
Example #4: Pre Decrement Operator
#include <iostream>
using namespace std;
int main()
{
int q, p = 10;
q = --p;
cout <<"The value of q is "<< q<<endl;
cout <<"The value of p is "<< p;
return 0;
}
Output:-
The value of q is 9
The value of p is 9
0 Comments