I'm having a hard time understanding postfix increment in this while loop.
I know prefix increment such as ++x for example . . .
x= 4 y= ++x
here the x will increment 4 to become 5 and then it will evaluate the x therefore now y is x
but if was postfix increment
x =4 y= x++
x is being evaluated first and then it gets increment. So now x is 5 but y is 4 now? why is that?
Not sure if this right, but my question is how is the cValue postfix being seen here? For example it's evaluating if the cValue is <= to char Z, but then it increments it so it becomes A? I'm really confuse if someone can explain, thanks.
/*
* Write a program that prints out the letters a through z along with their ASCII codes.
* Hint: to print characters as integers, you have to use a static_cast.
*/
#include <iostream>
usingnamespace std;
int main(){
char cValue = 'a';
while(cValue <= 'z'){
cout << cValue << " " << static_cast<int>(cValue) << "\n"<< endl;
cValue++;
}
return 0;
}
Kind of, in the example of the reading link I'm guessing cValue is the sequence point because it's between the evaluation and the expression? Since it's postfix incrementing meaning cValue is being evaluated first then it gets incremented by 1.
int& prefix_increment( int& v ) // yields an lvalue
{
v = v + 1 ; // increment v
return v ; // and return it
}
int postfix_increment( int& v ) // yields a prvalue (pure rvalue)
{
int copy_of_old_value = v ; // make a copy of v
v = v + 1 ; // increment v
return copy_of_old_value ; // return an anonymous temporary which holds
// a copy of the old value (the value *before* increment)
}
This was a great demonstration, thanks. I understand now, for the prefix increment function it's incrementing one and returns the new value, & postfix increment function it's duplicating the old value that's being evaluated and then it increments it by 1, returning the old value again that was duplicated.