Question on integer variables (c++)

Hello guys! It's orszhak and in my book I am currently studying incrementing values in c++ and it states thant I could do this to increment the value of nVariable

 nVariable = nVariable + 2; 

it states that I could also do this and assign the same value

 nVariable += 2;

but can't I also do this and assign the same value to the variable? here:

 nVariable = 2;

Or what about this

 int variable;
variable = 2; 

Anyways thanks guys! :slight_smile:

I'm guessing you're confused on the concept of increment. += 2 adds two, it doesn't set it to two.

#include <stdio.h>

int main(void)
{
        int variable;
        variable=0;

        printf("variable is %d\n", variable);

        variable += 2;
        printf("variable is %d\n", variable);
        variable = variable + 2;
        printf("variable is %d\n", variable);
        variable=2;
        printf("variable is %d\n", variable);
        return(0);
}

If that's not what you mean, I'm sorry, I don't understand your question.