Solved

While Loop Question

Posted on 2006-11-26
7
231 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 7

Expert Comment

by:UrosVidojevic
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
OK It is clearer now. Thank you very much indeed.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

744 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

20 Experts available now in Live!

Get 1:1 Help Now