Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

While Loop Question

Posted on 2006-11-26
7
Medium Priority
?
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 7

Accepted Solution

by:
UrosVidojevic earned 200 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

636 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