# A Question!

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 01-11-2006
A Question!
... If we have this segment of a program:
Code:

```int x=5, i=0; do {       i++;        x--; } while(x || x--); cout<<i;```
may anyone tell me wut does "while(x || x--)" mean??? the answer would be 5! but i don't know why!
• 01-11-2006
CornedBee
It's a demonstration of lazy evaluation of logical operators. (x || x--) effectively says:
Take x. If its value evaluates to true (i.e. is not 0), let the expression evaluate to true and stop.
Otherwise, take the next expression, x--. Evaluate it - this decrements x and yields the value it had before - and if it evaluates to false AGAIN (this is the only logical possibility in this case, but don't let that distract you), return false for the whole expression, otherwise true.

In other words, (expr1 || expr2) means, "either expr1 is true OR ELSE expr2 is true - but if expr1 is true, don't bother looking at expr2".

It's a rather poor example, because it obfuscates the important stuff with lots of decrements and a loop. Here's a simpler example:
Code:

```bool b1 = true || (std::cout << "Shortcut in first line not taken.\n"); bool b2 = false || (std::cout << "Shortcut in second line not taken.\n");```
• 01-11-2006
Tonto
It does not mean anything. || is left-to-right associative, and the x-- is never executed (short circuit evaluation occurs). x is decremented after it evaluates to a false 0, making it -1 however.
• 01-11-2006
Daved
The loop runs 5 times. After the fifth run, x is 0, so the first part of the while control evaluates to false. Then the second part (x--) is evaluated. It is also falso because it returns the value of x before decrementing it, which in this case is zero. So false or false is false and the loop exits. i is 5 because the loop ran 5 times, and x is -1 because it stopped when x was zero but the x-- decrement ran inside the while control the last time through.
• 01-12-2006
oh.. ok.. i got it perfectly well! bunch of thanks:D....
but i want to make sure of something else depending on the example above;
if we have this :
Code:

```int n=0, m=1; while(n) { ++n; m+=n; cout<<m<<" "<<n; }```
this is can't be right... right? cuz n=0 and while(0) it doesnt mean anything? so this program would not run? right?
• 01-12-2006
hk_mp5kpdw
Quote:

this is can't be right... right? cuz n=0 and while(0) it doesnt mean anything? so this program would not run? right?

No, it will run perfectly fine. It just won't produce any output since you would not enter the body of the loop.
• 01-12-2006
i didn't get it! :( in the while loop there z
Code:

`cout<<m<<" "<<n;`
if it runs then it sould print the values of both of them??? and if it runs wiit does while( 0) mean? and if replaced while(0) with while(++n) wut while(++n) mean????
• 01-12-2006
ZuK
The program will run. The loop will not be entered.
Kurt
• 01-12-2006
while(0) means wut? and wut does it differ from while(++n)?????
• 01-12-2006
Daved
The control of the while loop (what's between the parentheses) is a boolean expression that determines how long the loop executes and when it stops. A boolean expression evaluates to either true or false. If it evaluates to true, the loop is run, if it evaluates to false, the loop is not run.

If you put in 0, then it evaluates to false because in C++ 0 means false. That means the loop is not run. If you put in something like ++n, then before each time through the loop it will execute the code ++n and evaluate it to see if it is true or false. When at some point it becomes 0 then it evaluates to false and the loop stops.
• 01-12-2006
IfYouSaySo
Quote:

while(0) means wut? and wut does it differ from while(++n)?????
wut means wut? and how does wut differ from what?

Sorry, I couldn't help myself.
• 01-13-2006
i mean that if we have in any program this segment:
Code:

```int n=0; while(n){ ++n; cout<<n; }```
i meant wut does the while(n) do in this case? hope you got it now:)...
but i guess i understood it now:)...
thnx!
• 01-13-2006
Blackroot
Code:

`while(n);`
is the same as

Code:

`while(n==1);`
• 01-13-2006
CornedBee
No, it's not. It's the same as
Code:

`while(n != 0)`
• 01-13-2006
Blackroot
?
Code:

`while(n);`
is the same as

Code:

`while(n != 0)`
?

I thought it was the other way around...

Code:

```while(!n) // Same as while(n==0) while(n) //Same as while(n==1)```
I got that straight out of a C++ book -,-. I'm very much confused right now.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last