# Question on integer variables (c++)

**URL:** <https://community.unix.com/t/question-on-integer-variables-c/286326>\
**Category:** Programming\
**Created:** [April 13, 2011, 8:24pm UTC](https://community.unix.com/t/question-on-integer-variables-c/286326 "2011-04-13T20:24:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![orszhak](https://community.unix.com/letter_avatar/orszhak/32/6_449c533f586939b6e5f246632a46421f.png) [@orszhak](https://community.unix.com/u/orszhak)\
**Post date:** [April 13, 2011, 8:24pm UTC](https://community.unix.com/t/question-on-integer-variables-c/286326/1 "2011-04-13T20:24:07Z")

</div>

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

```nohighlight
 nVariable = nVariable + 2; 

```

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

```nohighlight
 nVariable += 2;

```

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

```nohighlight
 nVariable = 2;

```

Or what about this

```nohighlight
 int variable;
variable = 2; 

```

Anyways thanks guys! 🙂

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [April 13, 2011, 8:42pm UTC](https://community.unix.com/t/question-on-integer-variables-c/286326/2 "2011-04-13T20:42:29Z")

</div>

> [@orszhak](#):
>
> 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
> 
> ```plaintext
> nVariable = nVariable + 2; 
> 
> ```
> 
> it states that I could also do this and assign the same value
> 
> ```plaintext
> nVariable += 2;
> 
> ```
> 
> but can't I also do this and assign the same value to the variable? here:
> 
> ```plaintext
> nVariable = 2;
> 
> ```
> 
> Or what about this
> 
> ```plaintext
> int variable;
> variable = 2; 
> 
> ```
> 
> Anyways thanks guys! 🙂

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

```plaintext
#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.
