+ 8
I felt difficulty in solving coding problems on pre and post increment problems
difference in pre/ post increment operator problem solving technique.
10 Respostas
+ 8
thanks to all for nice explanations
+ 7
@JoJo - thanks for the clarification. I kinda get what your saying, and I understand incrementation a bit more now because of it, but I think I'll have to experiment with it in some practice codes to really grasp it fully.
+ 4
I also had some difficulty with this. I get the concept, but where specifically does "i" get incremented when, let's say,
a = i++
People have said in their answers for this question, after the next "statement", or after the next "sentence"; is this referring to the next line of code? And I think someone said, the next time "i" is used.
Someone please explain specifically where the variable gets incremented when using post-incrementation.
+ 3
thanks @jojo and Boo DaMan
+ 2
Let i=2
++i means increment before execution of sentence. so a=++i
results a=3.
Now again i=2
i++ indicates increment after execution of sentence. a=i++ will make a=2 and will increment i. so i becomes 3
+ 2
small suggestion: avoid loosing to much time on
things like "what's the result of ++a+++b++.....".
things like that are never used and considered bad practice as their solutions are compiler dependant.
+ 2
When you write :
int x=0, y;
y=(--x)+(--x)+(--x);
C++ thinks this :
y=(--x)+(--x);
y=y+(--x);
Soooo :
y=(--x)+(--x) ;// -2+-2=-4
y=y+(--x);// -4+-3=-7
--x //-1
--x //-2
x+x // x=-2
--x //-3
-2+x // x=-3
inctementation is fantastic but it becomes quick very very complicated as you can see.
If you don't understand ask me, don't hesitate!
+ 1
++i increment i's value before anything :
int a=5, i=4;
cout << a==++i; // Same as:
i+=1;
cout << a==i;
i++ increment i's value after ONE statement, the one where i is used:
int a=5, i=4;
cout << a==i++; // same as:
cout << a==i;
i+=1;
If you have other questions on incrementation ask !
+ 1
I think what they forget to mention is that the first variable then changes. I found it hard to get my head around i=3. if you imagine
i=2
a = ++i
to me this means a is 3 but i is still 2. But No, the ++ increments i aswell.
So both i and a are equal to 3
+ 1
You're right the term 'statement' must be defined.
In my answer, a 'statement' is an 'action' accomplished by the compiler :
a=1; is one statement.
a=a+1; is composed of two statements in C++.
Let say a=1.
C++ read this :
a+1 ---> temp // I used '--->' to show that C++ remembers the value of 'a+1'
So this was the first statement.
then :
a=temp //remember that 'temp' is not a real variable, I used it to show how it works.
This is the second statement.
In other words :
a=a+1;//C++ evaluate first a+1. Then, it evaluates a=... (here it is the value of 'a+1')
Ask if you don't understand