code is

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int count=1;
    while (count++ <=5)
    cout<<count*(count-2)<<" ";
    cout<<endl;

}

WHat I don't get is how count becomes 3,8,15,24...thanks!

another one is

int count=5;
while (count-- > 0)
cout<<count<<" ";
cout<<endl;

Why the output is showing "0" as well when we set the condition of >0?thanks

First off doing increment in a loop is alot easier with a for loop, which is designed for that.

In the first instance, the numbers you are seeing are the result of count*(count-2), not the value of count

In the second instance count is compared to 0 before it is decremented, so when count = 0, it is dercremented before it is compared again.

To re-iterate this kind of confusion is easier to avoid using a for loop whenever you have a closed loop and/or need the incremented value for an expression. to wit:

        for(int count = 2;count <= 6; count++)
        {
             cout<<count*(count-2)<<" ";
        }
            cout<<endl;

produces the same output, but it is a lot easier to see what the values of count will be, namely each integer from 2 to 6 inclusive.

sir thanks for replying..I get about the second one but still confusion about first one..how is

cout<<count*(count-2)<<" "; 

equals 3 after loop runs for the first time?shouldn't it be 2*(2-2)=0?

What compiler are you using?
The output I get from VS 2012 is: 0 3 8 15 24

i get the same but I don't get logically how it should be 3?

With your original code, the total output is 0 3 8 15 24. The 0 is the result of the first run. Since count starts off equal to 1 then it is incremented so count = 2 and the expression result is 0 (2 * (2 - 2)), the next increment makes count = 3 so the expression will return 3 (3 * (3 - 2)). And so on.

using G++ (GCC) 4.5.2 the result are:
0 3 8 15 24

I will interpret the code

1) count = 1;
2) loop while count <= 5;
3) count = count + 1;    // the value of count is 2
4) val1 = count - 2;     // the value of val1 is 0
5) val2 = count x val1;  // the value of val2 is 0
6) print val2
7) goto #2

Invadev

While loop is working due to when the value of variable less then 5... When variable's value is equal to 5 then the program will automaticaly terminate.

it should be 3 as 3 is the result after second iteration of the loop body. In the second iteration value of count is already 2 when the condition statement is processed. So when entering the loop body, the value of count will get incremented to 3 as count++ is used. And the result is displayed after calculating (count(count - 2)) which is logically equivalent to (3(3-2)) which equals 3.

He he he. Actually, the OP's confusion is valid. After the state while( count++<=5), count's value will already be 2, thus cout<<count*(count-2)<<" "; will be 2(2-2)==>2(0)==>0. It's like the sum(A1:A5) function in excel returning different result compared to A1+A2+A3+A4+A5.

If you're very much sure there's nothing to check in your codes, then there might be something wrong with your compiler.

Why the problem cannot be with the logic???

First loop is actually 2x(2-2) which will result to 0. But why is your compiled program returning 3?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.