Home > other >  Why adding "variable 1" doesn't increment its value by one for every loop in a for
Why adding "variable 1" doesn't increment its value by one for every loop in a for

Time:10-26

I'm having a hard time understanding why using a increment operator in a for loop in C has a different result than doing 'variable' 1. The variable in the second case simply isn't remembered after each iteration of the loop:

Take the following code:

#include <iostream>

int main(){
    int a{0};
    for (int i = 0; i < 5; i  ){
        std::cout <<   a;
    }
return 0;
}

It outputs as expected: 12345

However, if I instead replace a with a 1:

#include <iostream>

int main(){
    int a{0};
    for (int i = 0; i < 5; i  ){
        std::cout << a   1;
    }
return 0;
}

I get: 11111

Even if I make the 'a' variable static:

static int a{0};

It still outputs 11111.

Why doesn't 'a 1' preserve its value after every loop? What would I need to do if I wanted to preserve its value after every iteration without using (for instance, with another operation like a * 2)?

CodePudding user response:

Why doesn't 'a 1' preserve its value after every loop?

a 1 is an expression that doesn't assign anything to a. That is a is not affected in any way. This means when you do just a 1, the value of a is still 0(the old value).

On the other hand a has the same effect as:

  v---------->assignment done here implicitly
a = a 1

In the above expression a, the value of is incremented by 1 so that the new value of a is now 1.

CodePudding user response:

a is equivalent to the assignment a= a 1 (or a = 1). The expression a 1 alone does not modify a.

C will allow you to write std::cout << (a= a 1);.

CodePudding user response:

a is not the same thing as a 1 but as a=a 1.
What's the difference?

Well:
a 1 contains the value, which is one higher than the value of a.
a=a 1 means that, in top of this, this value gets assigned to the variable a, which means something like "replace a by its value plus one".

This gives following possibilities (I also show the difference between a and a):

int a=3;
cout<<a 1; // output : 4. Value of 'a' equals 3.
cout<<a 1; // output : 4. Value of 'a' equals 3.
cout<<a 1; // output : 4. Value of 'a' equals 3.

int a=3;
cout<<a  ;  // output : 3. Value of 'a' becomes 4 after having shown it on screen.
cout<<a  ;  // output : 4. Value of 'a' becomes 5 after having shown it on screen.
cout<<a  ;  // output : 5. Value of 'a' becomes 6 after having shown it on screen.

int a=3;
cout<<  a;  // output : 4. Value of 'a' becomes 4 before showning it on screen.
cout<<  a;  // output : 5. Value of 'a' becomes 5 before showning it on screen.
cout<<  a;  // output : 6. Value of 'a' becomes 6 before showning it on screen.

CodePudding user response:

Conceptually, the built-in arithmetic operators like and * evaluate their operands, perform the respective operation with the thusly obtained values, and create a temporary object that contains the result.

The operands, one of which is your a, are only read from, not written to. That is more obvious when you consider that is commutative, that is, its operands can be swapped without affecting the result: a 1 is by definition the same as 1 a, and clearly you cannot write anything back to the immediate value 1.

Generally, as you certainly know, = is used in C and C to write a value to a variable. C, and by means of ancestry also C , provide shortcuts to write back the result of certain operations to one of their operands: it's the combination of the operator and the assignment =, for example a = 1. Like all assignments, this one is an expression (that is, it has a value) which has the value of the variable after the operation and assignment. Like all other expressions, you can use it as a subexpression, even though that's not very common: cout << (a = 1); would achieve the desired effect (the parentheses are necessary because assignment has about the lowest operator precedence). Because incrementing (and decrementing) by one is so very common, C and C have a shortcut for the shortcut: a is the same as a =1, so that one would typically write cout << a; (as a side effect obviating the need for parentheses).

  • Related