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

C++ में Increment और Decrement

Increment और decrement एक जोड़ते या घटाते हैं, एक tally counter click करने जैसा। यह मायने रखता है कि change value इस्तेमाल होने से पहले होता है या बाद में।
Syntax
cpp
variable++;  // postfix: use value, then increase
++variable;  // prefix: increase, then use value
variable--;
--variable;

Postfix Increment Operator

Postfix x++ पहले आस-पास के expression में इस्तेमाल के लिए x की current value return करता है, और सिर्फ बाद में actual variable को 1 बढ़ाता है।

यह distinction तब मायने रखती है जब operator inline इस्तेमाल हो, जैसे arr[i++], जो i increment होने से पहले index i access करता है।

उदाहरण: Postfix Increment Operator

cpp
#include <iostream>

int main() {
	int arr[3] = {10, 20, 30};
	int i = 0;
	std::cout << arr[i++] << std::endl; // accesses index 0, then i becomes 1
	std::cout << i << std::endl;
	return 0;
}

Prefix Increment Operator

Prefix ++x तुरंत variable को बढ़ाता है और फिर वह already-updated value return करता है, यही वजह है कि cout << ++x; incremented value दिखाता है, जबकि cout << x++; increment से पहले वाली value दिखाएगा।

x++; जैसे simple standalone statements के लिए अपनी line पर, prefix और postfix identically behave करते हैं क्योंकि कुछ भी returned value इस्तेमाल नहीं करता।

उदाहरण: Prefix Increment Operator

cpp
#include <iostream>

int main() {
	int x = 5;
	std::cout << ++x << std::endl; // shows the incremented value: 6
	return 0;
}

Postfix Decrement Operator

Postfix x-- postfix increment को mirror करता है: यह immediate इस्तेमाल के लिए current value return करता है, फिर बाद में variable घटाता है।

यह ordering उन loops में important है जो नीचे count करते हुए pre-decrement value भी इस्तेमाल करते हैं, जैसे अगले पर move करने से पहले एक array के last element को process करना।

उदाहरण: Postfix Decrement Operator

cpp
#include <iostream>

int main() {
	int x = 5;
	std::cout << x-- << std::endl; // shows 5, then x becomes 4
	std::cout << x << std::endl;
	return 0;
}

Prefix Decrement Operator

Prefix --x पहले variable घटाता है और already-reduced value return करता है, prefix ++x का decrement counterpart।

Increment की तरह, prefix/postfix distinction actually तभी behavior बदलता है जब operator की return value same expression में कहीं और इस्तेमाल हो।

उदाहरण: Prefix Decrement Operator

cpp
#include <iostream>

int main() {
	int x = 5;
	std::cout << --x << std::endl; // decreases first, shows 4
	return 0;
}

Outputs के अंदर Operators

इन operators को cout statements के अंदर test करना prefix/postfix फर्क खुद देखने का एक quick तरीका है: cout << x++ << " " << x; original value के बाद incremented वाली print करेगा, update की timing को सिर्फ theoretical की बजाय visible बनाते हुए।

उदाहरण: Operators inside Outputs

cpp
#include <iostream>

int main() {
	int x = 5;
	std::cout << x++ << " " << x << std::endl; // prints "5 6"
	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++ और ++x एक बड़े expression में same behave करते हैं, जब postfix पुरानी value return करता है और prefix नई।
  2. x = x++; लिखना, जो x को बिना clear order के दो बार modify करता है और पुराने standards में undefined behavior है।
  3. a[i] = i++; जैसे एक statement में i++ और i साथ इस्तेमाल करना, जिसका result पुराने standards में unspecified है।

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.