Solved

While Loop Question

Posted on 2006-11-26
7
238 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:MacroLand
  • 3
  • 3
7 Comments
 
LVL 7

Accepted Solution

by:
UrosVidojevic earned 50 total points
ID: 18013977
Hi,

>> 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

in case of operator ++ it depends.

1.) if you use preincrement operator (++k). then C++ first increments k, and then use it in any expression that ++k is in.
2.) if you use postincrement operator (k++). then C++ use k for computing the expression that k++ is in, and afther that increments k.

In your piece of code you use postincrement operator and thats why you have k = 12 value at the end of loop.

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!

If you have used ++k, you wold have k = 11 at the end.

value before                increment                     comparation
comparation
k = 0,                          k = k + 1 = 1                k = k + 1 = 1;
k = 1,                          k = k + 1 = 2                k = k + 1 = 2;
...
k = 9,                          k = k + 1 = 10              k= k + 1 = 10;
k = 10,                        k = k + 1 = 11              (11 <= 10), false, end of loop.

For second question, I have really no idea :-).
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 18013981
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.
0
 
LVL 4

Author Comment

by:MacroLand
ID: 18014128
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 18014204
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).
0
 
LVL 4

Author Comment

by:MacroLand
ID: 18014307
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!!!
0
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 18014633
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!
0
 
LVL 4

Author Comment

by:MacroLand
ID: 18015385
OK It is clearer now. Thank you very much indeed.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now