Link to home
Start Free TrialLog in
Avatar of MacroLand
MacroLandFlag for United States of America

asked on

While Loop Question

Hi Friends,

I have a simple question but I couldnt figure it out whats going on...

I have a piece of code like

int k=0;
while(k++<=10)
      cout<<k<<endl;

cout<<"After loop k="<<k;

The printed values are 1,2,3,4,5,6,7,8,9,10,11

and after the loop the value of k is 12.

How does C++ test the expression? Does it increase the value of k then test the expression or does it first test then increase the value of k


Secondly,

if you add QuickWatch for expressions k++<=10 and k the output is 3,6,9,12 and after loop the value of k=15

What is going on?


By the way I am using Microsoft Visual Studio.NET
ASKER CERTIFIED SOLUTION
Avatar of UrosVidojevic
UrosVidojevic
Flag of Serbia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AlexFM
AlexFM

k++
First k value is tested, then incremented.

++k
First k value is incremented, then tested.

I guess QuickWatch increments k during expression evaluation - good example of debugger which changes program results. Don't use QuickWatch expressions which change variable values.
Avatar of MacroLand

ASKER

Still have a little question.

Is it inherent to loops that even after exiting the loop, the value of the variable, in this case k, always increases.
Loop can't change variable after exiting from it!
Every loop in C++ has some condition that is neccessary to be true in order to begin next iteration.

example:

while (this_condition) {}

for (...; this_condition; ...) {}

do {} while (this_condition);

note that this_condition is tested before / after(do..while loop) each iteration. So if you have some change on your variable in this_condition than it will be done each time when this_condition is true, even when the loop ends (this_condition is false in this case).
I get your point but

value before                comparation                     increment
comparation
k = 0,                          (0<=10),                            k = k + 1 = 1;
k = 1,                          (1<=10),                            k = k + 1 = 2;
...
k = 9,                          (9 <=10),                           k= k + 1 = 10;
k = 10,                        (10 <= 10),                        k = k + 1= 11;
k = 11,                        (11 <= 10);                        this is the end of loop, but you still have k = k+1 = 12!

comparation is false but still the value increases!!!
It is not important whether comparation is true or false, k++ is in this comparation and therefore k increases.

in the last iteration you have:

k is 11,
(k++ <= 10) is (11 <= 10), which is false,
but C++ now must increment k (k is 12 now), because k++ was in expression.
As I already said, compailer saw k++ in comparation and therefore he must increment k. That is totally independent of value of comparation and must be done in both (true and false) cases!
OK It is clearer now. Thank you very much indeed.