Increment and Decrement

difference between this

x++ and ++x

x-- and --x
Postfix (where the operator comes after the variable) increment/decrement return the value of the variable before the increment/decrement.
Prefix (where the operator comes before the variable) increment/decrement return the value of the variable after the increment/decrement.

All this assumes that those operators haven't been overloaded screwily. It may seem a bit backward, but it might help to think of it this way:

Postfix: Return the value of the variable, then increment/decrement.
Prefix: Increment/decrement, then return the value of the variable.

-Albatross
hmmm

check this

for (n=0 ; n<5 ; n++)

for (n=0 ; n<5 ; ++n)

what is the difference of the two? tnx

is n an int? No difference here, because you do nothing with the result of the operation.

By the way, it's usually cleaner to define the loop variable from within the scope of the loop itself.
(int n = 0; n < 5; n++)
Last edited on
oops i forgot about int


i confuse

some loops use n++ and some use ++n can you tell me why?
what is the difference of the two?
Doing a quick test should reveal the differences, if any:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
   for (size_t loop = 0; loop < 5; ++loop)
   {
      std::cout << loop << ' ';
   }
   std::cout << "\n\n";

   for (size_t loop = 0; loop < 5; loop++)
   {
      std::cout << loop << ' ';
   }
   std::cout << '\n';
}

0 1 2 3 4

0 1 2 3 4

Well, looks like when the increment is executed each iteration through the loop is after the loop's block code, so......

An increment in a for loop doesn't matter if it is pre- or post- fix.
It's just user preference.
As long as nothing is being done with the return value of the operation, it doesn't matter.

Some may claim that ++n is more efficient because n++ technically creates a copy, but any decent compiler will turn both into the same operation, so just don't worry about this.

You can see that here using the Godbolt compiler explorer:
https://godbolt.org/z/nx5NU_
Both end up just doing add ebx, 1 meaning 1 is added to a registry.
(Or, without optimization, both end up doing add DWORD PTR [rbp-8], 1, but it's the same assembly for both functions either way.)

If the variable is a custom class that overloads the ++ operator, then you might want to be more careful about it, because potentially logic might force a copy to be made. But probably not.
Last edited on
Owww ok tnx sirr


can you give some simple example

when this two (n++ and ++n) have difference?
When you actually use the return value of the result of the ++ operation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Example program
#include <iostream>

void foo_A()
{
    int n = 5;
    int result = n++; // n gets incremented from 5 to 6, returns 5
    
    std::cout << result << '\n';
}

void foo_B()
{
    int n = 5;
    int result = ++n; // n gets incremented from 5 to 6, returns 6
    
    std::cout << result << '\n';
}

int main()
{
    foo_A();
    foo_B();
}
Last edited on
1
2
3
4
5
6
7
#include <iostream>
int main() {
    int var = 1;
    std::cout << ++var << std::endl; //Prints 2 (the value after the increment).
    std::cout << var++ << std::endl; //Prints 2 (the value before the increment).
    std::cout << var << std::endl; //Prints 3.
}


-Albatross
So n++ is not permanent?

because you increment 6 to it but it returns 5

Sorry if im sound idiot AHAHAHAH i just want to understand well

I am naive actually
Last edited on
When you call n++, two things happen
(1) The value of n is returned, before n is incremented
(2) n itself is incremented by 1. This is "permanent". n will have a value of +1 after the operation.

When you call ++n, two things happen
(1) n is incremented by 1. Again, permanent.
(2) The value of n (now incremented by 1) is returned.
Last edited on
Oww okayy thanks alot albatross and ganado

btw ganado are you pinoy?
No, sorry.
Keighhhh
Registered users can post here. Sign in or register to post.