← Back to C++ Course | Chapter 3: Operators | Lesson 6 of 9

C++ के Assignment Operators

Assignment operators एक variable में एक value रखते हैं, और short versions इसे अपनी पुरानी value से update करते हैं, जैसे 'मेरे score में 5 जोड़ो'। ये आपको नाम दोहराने से बचाते हैं।
Syntax
cpp
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

cpp
#include <iostream>

int main() {
	int x;
	x = 10; // copies 10 into x
	std::cout << x << std::endl;
	return 0;
}

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

cpp
// 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;
}

Subtraction Assignment Operator

-= subtraction के लिए same तरीके से काम करता है, एक single statement में एक variable को दी गई मात्रा से घटाते हुए — एक countdown timer decrement करने या एक purchase के बाद inventory count घटाने जैसी चीज़ों के लिए useful।

stock -= sold; लिखना पूरी तरह spelled-out version से छोटा और ज़्यादा readable दोनों है।

उदाहरण: Subtraction Assignment Operator

cpp
// 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;
}

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

cpp
#include <iostream>

int main() {
	double price = 100.0;
	price *= 1.1; // apply a 10% increase
	std::cout << price << std::endl;
	return 0;
}

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

cpp
#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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. x += 5 का मतलब होने पर x =+ 5 लिखना; यह add करने की बजाय +5 assign करता है।
  2. ints के साथ /= इस्तेमाल करना और decimals की उम्मीद करना, जैसे int x = 5; x /= 2; 2 देता है।
  3. गलती से x =+ 1 या =- इस्तेमाल करना, क्योंकि sign को value का हिस्सा माना जाता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.