Solved

While Loop Question

Posted on 2006-11-26
7
247 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

831 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