C++ में Increment और Decrement
In this page:
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <iostream>
int main() {
int x = 5;
std::cout << ++x << std::endl; // shows the incremented value: 6
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <iostream>
int main() {
int x = 5;
std::cout << --x << std::endl; // decreases first, shows 4
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Outputs के अंदर Operators
इन operators को cout statements के अंदर test करना prefix/postfix फर्क खुद देखने का एक quick तरीका है: cout << x++ << " " << x; original value के बाद incremented वाली print करेगा, update की timing को सिर्फ theoretical की बजाय visible बनाते हुए।
उदाहरण: Operators inside Outputs
#include <iostream>
int main() {
int x = 5;
std::cout << x++ << " " << x << std::endl; // prints "5 6"
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि
x++और++xएक बड़े expression में same behave करते हैं, जब postfix पुरानी value return करता है और prefix नई। x = x++;लिखना, जोxको बिना clear order के दो बार modify करता है और पुराने standards में undefined behavior है।a[i] = i++;जैसे एक statement मेंi++औरiसाथ इस्तेमाल करना, जिसका result पुराने standards में unspecified है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: