C++ के Assignment Operators
In this page:
variable = value;
variable += value; // also -= *= /= %=
Basic Assignment Operator
= operator इसके right पर एक expression से जो भी value result आती है उसे लेता है और इसे left पर variable में copy करता है, वहाँ पहले जो भी store था उसे overwrite करते हुए।
यही mechanism आपके program द्वारा track किए हर piece of state के पीछे है, एक running total से लेकर user की chosen setting तक।
उदाहरण: Basic Assignment Operator
#include <iostream>
int main() {
int x;
x = 10; // copies 10 into x
std::cout << x << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Addition Assignment Operator
+= addition और assignment को एक step में fold करता है, इसलिए score += 10; 'score को 10 से बढ़ाओ' की तरह पढ़ता है और exactly same result produce करता है जो score = score + 10; कम typing के साथ करता है।
यह loops में खासकर handy है, जहाँ एक running total लगभग हर iteration पर update होता है।
उदाहरण: Addition Assignment Operator
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare score and set it to 0
int score = 0;
score += 10;
// Print to the console with cout
std::cout << score << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Subtraction Assignment Operator
-= subtraction के लिए same तरीके से काम करता है, एक single statement में एक variable को दी गई मात्रा से घटाते हुए — एक countdown timer decrement करने या एक purchase के बाद inventory count घटाने जैसी चीज़ों के लिए useful।
stock -= sold; लिखना पूरी तरह spelled-out version से छोटा और ज़्यादा readable दोनों है।
उदाहरण: Subtraction Assignment Operator
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare stock and set it to 50
int stock = 50;
// Declare sold and set it to 5
int sold = 5;
stock -= sold;
// Print to the console with cout
std::cout << stock << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Multiplication और Division Assignment Operators
*= और /= multiplication और division पर same shorthand pattern apply करते हैं, आपको एक variable को in place scale up या down करने देते हुए, जैसे price *= 1.1; 10% increase apply करने के लिए।
क्योंकि ये operators calculation के हिस्से के तौर पर variable की current value इस्तेमाल करते हैं, कई updates साथ chain करते समय order मायने रखता है।
उदाहरण: Multiplication and Division Assignment Operators
#include <iostream>
int main() {
double price = 100.0;
price *= 1.1; // apply a 10% increase
std::cout << price << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Modulo Assignment Operator
%= एक variable को एक दी गई number से divide करता है और इसे सिर्फ remainder से replace करता है, जो एक limit तक पहुँचने पर एक counter को वापस zero पर wrap करने के लिए useful है, जैसे एक index को valid bounds के अंदर cycling रखने के लिए index %= arraySize;।
यह pattern circular buffers और round-robin scheduling logic में अक्सर दिखता है।
उदाहरण: Modulo Assignment Operator
#include <iostream>
int main() {
int index = 4;
int arraySize = 5;
index = (index + 1);
index %= arraySize; // wraps back within bounds
std::cout << index << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
x += 5का मतलब होने परx =+ 5लिखना; यह add करने की बजाय +5 assign करता है।- ints के साथ
/=इस्तेमाल करना और decimals की उम्मीद करना, जैसेint x = 5; x /= 2;2 देता है। - गलती से
x =+ 1या=-इस्तेमाल करना, क्योंकि sign को value का हिस्सा माना जाता है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: